babesz babesz - 2 months ago 6
R Question

How to recount rows in the first column?

I have a data frame where I want to drop some rows, and I've found a fairly elegant way to do this:

Df2=Df[-c(1,2,11),]


however, when I call the data with the head() function, I notice that in the 1st column the rows start from 3,4,5, etc

I also notice that if I export my data into csv and import it again, there is the old column (starting from 3) and a new one appears as well (starting from 1,2,3) which I find redundant.

Is there any way I can tell R to recount the rows of the old column before saving it?

Answer

Summarizing the answers given by Vandenman and Rich Scriven:

Either

rownames(Df2) = 1:nrow(Df2) 
row.names(Df2) = 1:nrow(Df2)

or

rownames(Df2) <- NULL
row.names(Df2) <- NULL

or

Df2 <- data.frame(Df2, row.names = NULL)
Df2 <- as.data.frame(Df2, row.names = NULL)

will do the job