How to delete the first row of data in R?

I have a data set with 11 columns with over 1000 rows each. Columns were designated as V1, V2, V11, etc. I replaced the names with something more useful to me using the "c" command. I did not realize that line 1 also contains labels for each column, and my actual data starts on line 2.

Is there a way to remove row 1 and reduce it?

+60
r dataset
Sep 24 '11 at 20:11
source share
5 answers

Store the tags in the source file as follows:

df = read.table('data.txt', header = T) 

If you have columns with the names x and y, you can address them as follows:

 df$x df$y 

If you want to remove the first row from data.frame, you can use negative indexes, for example:

 df = df[-1,] 

If you want to remove a column from data.frame, you can set it to NULL:

 df$x = NULL 

Here are some simple examples of how to create and process 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 yx 1 0.66658148 -0.95343778 2 0.15355851 -0.63098637 3 0.60098886 -1.30646529 4 0.14284576 1.38906143 5 0.20408723 0.51703237 6 0.58271061 -0.02246754 7 0.05170994 0.20583548 8 0.83627336 0.21530721 9 0.76713317 0.69087460 10 0.95052671 2.30610998 > df[-1,] yx 2 0.15355851 -0.63098637 3 0.60098886 -1.30646529 4 0.14284576 1.38906143 5 0.20408723 0.51703237 6 0.58271061 -0.02246754 7 0.05170994 0.20583548 8 0.83627336 0.21530721 9 0.76713317 0.69087460 10 0.95052671 2.30610998 > df$x = NULL > df y 1 0.66658148 2 0.15355851 3 0.60098886 4 0.14284576 5 0.20408723 6 0.58271061 7 0.05170994 8 0.83627336 9 0.76713317 10 0.95052671 
+103
Sep 24 '11 at 20:17
source share

You can use negative indexing to delete rows, for example:

 dat <- dat[-1, ] 

Here is an example:

 > dat <- data.frame(A = 1:3, B = 1:3) > dat[-1, ] AB 2 2 2 3 3 3 > dat2 <- dat[-1, ] > dat2 AB 2 2 2 3 3 3 

However, you may have more problems than just deleting shortcuts that fall into line 1. Most likely, R interprets the data as text and then converts to factors. Check that str(foo) , where foo is your data object, is talking about data types.

It looks like you just need header = TRUE in your call to read data (assuming you read it through read.table() or one of them.)

+19
Sep 24 '11 at 20:17
source share

Nobody probably really wants to delete row 1. Therefore, if you are looking for something meaningful, this is conditional selection

 #remove rows that have long length and "0" value for vector E >> setNew<-set[!(set$length=="long" & set$E==0),] 
+11
Oct 26 '14 at 19:21
source share

dat <- dat[-1, ] worked, but it killed my data framework by changing it to another type. I had to use dat <- data.frame(dat[-1, ]) , but this is probably a special case, since in this part of the data at first there was only one column.

+3
Nov 05 '15 at 11:54 on
source share

I'm not an expert, but it might work,

 dat <- dat[2:nrow(dat), ] 
+2
Feb 23 '16 at 15:32
source share



All Articles