Select column 2 to last column in R

前端 未结 3 1842
被撕碎了的回忆
被撕碎了的回忆 2020-12-10 12:03

I have a data frame with multiple columns. Now, I want to get rid of the row.names column (column 1), and thus I try to select all the other columns.

E.g.,

相关标签:
3条回答
  • 2020-12-10 12:54

    To build on Freeman's answer, Tidyverse allows dots as replacement for the piped data object, which can be useful to simplify code with repetitive references to the object.

    library(tidyverse)

    newdata <- olddata %>% select( 2:ncol(.) )

    or

    newdata <- olddata %>% .[,2:ncol(.)]

    0 讨论(0)
  • 2020-12-10 13:02

    I think it's better to focus on wanting to get rid of one column of data and not wanting to select every other column. You can do this as @Arun suggested:

    olddata[,-1]
    

    Or:

    olddata$ColNameToDelete <- NULL
    
    0 讨论(0)
  • 2020-12-10 13:04

    You can get the last column with ncol():

     newdata <- olddata[,2:ncol(olddata)]
    
    0 讨论(0)
提交回复
热议问题