How to delete the first row of a dataframe in R?
Keep the labels from your original file like this:
df = read.table('data.txt', header = T)
If you have columns named x and y, you can address them like this:
df$xdf$y
If you'd like to actually delete the first row from a data.frame, you can use negative indices like this:
df = df[-1,]
If you'd like to delete a column from a data.frame, you can assign NULL to it:
df$x = NULL
Here are some simple examples of how to create and manipulate a data.frame in R:
# create a data.frame with 10 rows> x = rnorm(10)> y = runif(10)> df = data.frame( x, y )# write it to a file> write.table( df, 'test.txt', row.names = F, quote = F )# read a data.frame from a file: > read.table( df, 'test.txt', header = T )> df$x [1] -0.95343778 -0.63098637 -1.30646529 1.38906143 0.51703237 -0.02246754 [7] 0.20583548 0.21530721 0.69087460 2.30610998> df$y [1] 0.66658148 0.15355851 0.60098886 0.14284576 0.20408723 0.58271061 [7] 0.05170994 0.83627336 0.76713317 0.95052671> df$x = x> df y x1 0.66658148 -0.953437782 0.15355851 -0.630986373 0.60098886 -1.306465294 0.14284576 1.389061435 0.20408723 0.517032376 0.58271061 -0.022467547 0.05170994 0.205835488 0.83627336 0.215307219 0.76713317 0.6908746010 0.95052671 2.30610998> df[-1,] y x2 0.15355851 -0.630986373 0.60098886 -1.306465294 0.14284576 1.389061435 0.20408723 0.517032376 0.58271061 -0.022467547 0.05170994 0.205835488 0.83627336 0.215307219 0.76713317 0.6908746010 0.95052671 2.30610998> df$x = NULL> df y1 0.666581482 0.153558513 0.600988864 0.142845765 0.204087236 0.582710617 0.051709948 0.836273369 0.7671331710 0.95052671
You can use negative indexing to remove rows, e.g.:
dat <- dat[-1, ]
Here is an example:
> dat <- data.frame(A = 1:3, B = 1:3)> dat[-1, ] A B2 2 23 3 3> dat2 <- dat[-1, ]> dat2 A B2 2 23 3 3
That said, you may have more problems than just removing the labels that ended up on row 1. It is more then likely that R has interpreted the data as text and thence converted to factors. Check what str(foo)
, where foo
is your data object, says about the data types.
It sounds like you just need header = TRUE
in your call to read in the data (assuming you read it in via read.table()
or one of it's wrappers.)