How to delete the first row of a dataframe in R?

592    Asked by DavidWHITE in Data Science , Asked on Jun 16, 2021

 I have a dataset with 11 columns with over a 1000 rows each. The columns were labeled V1, V2, V11, etc. I replaced the names with something more useful to me using the "c" command. I didn't realize that row 1 also contained labels for each column and my actual data starts on row 2.

Is there a way to remove first row in r?

Answered by Benjamin Moore

To delete or remove first row in r data frame, you can use the negative indices as follows:

  data_frame = data_frame[-1,]

To keep labels from your original file, do the following:

data_frame = read.table('data.txt', header = T)
To delete a column:
data_frame$column_name = NULL
For example:
 x = rnorm(10)
 y = runif(10)
df = data.frame( x, y )
df
             x y
1 -1.74553874 0.24904310
2 0.78970091 0.40401348
3 0.86203201 0.50361443
4 -0.02194766 0.19179475
5 1.07919386 0.70638636
6 -0.23485948 0.41544163
7 0.07170211 0.08740739
8 -2.18997110 0.63813427
9 0.68096662 0.59799302
10 0.05319090 0.13227670
df[-1,] #delete 1st row
             x y
2 0.78970091 0.40401348
3 0.86203201 0.50361443
4 -0.02194766 0.19179475
5 1.07919386 0.70638636
6 -0.23485948 0.41544163
7 0.07170211 0.08740739
8 -2.18997110 0.63813427
9 0.68096662 0.59799302
10 0.05319090 0.13227670
df$y <- NULL #delete column “y”
df
             x
1 -1.74553874
2 0.78970091
3 0.86203201
4 -0.02194766
5 1.07919386
6 -0.23485948
7 0.07170211
8 -2.18997110
9 0.68096662
10 0.05319090

Your Answer

Interviews

Parent Categories