How can I get a dataframe with columns temporarily removed by name?

前端 未结 2 1713

For example, with the data set mtcars

mtcars[ , \"cyl\"]

and

mtcars[ , 2]

both give me the

2条回答
  •  无人及你
    2021-01-01 12:42

    I often use subset. An example using mtcars

    > names(mtcars)
     [1] "mpg"  "cyl"  "disp" "hp"   "drat" "wt"   "qsec" "vs"   "am"   "gear" "carb"
    > cars <- subset(mtcars, select=-c(mpg,cyl))
    > names(cars)
    [1] "disp" "hp"   "drat" "wt"   "qsec" "vs"   "am"   "gear" "carb"
    

    There are some other ideas in the answers to this question.

    Update: Subset also works for temporary removal of one or more columns by name, just replace mtcars[,-2] with subset(mtcars, select=-cyl).

提交回复
热议问题