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

前端 未结 6 1284
长情又很酷
长情又很酷 2020-11-30 22:34

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\

6条回答
  •  感动是毒
    2020-11-30 22:54

    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 B
    2 2 2
    3 3 3
    > dat2 <- dat[-1, ]
    > dat2
      A B
    2 2 2
    3 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.)

提交回复
热议问题