Deselecting a column by name

心不动则不痛 提交于 2019-12-03 18:34:19

问题


Is there a way to select all columns of a data frame except a column that has a particular name.

It would be the analog of df[, -1], except using the column name instead of the index?


回答1:


You can do this using vector subsetting. First, create a dummy data set:

R> dd = data.frame(A = 1:3, B = 1:3, C=1:3, D=1:3)

Then use the ! operator to reverse the selection:

R> dd[ ,!(colnames(dd) == "A")]

  B C D
1 1 1 1
2 2 2 2
3 3 3 3

Alternatively, you could have:

  • A slightly shorter version (courtesy of @Tomas):

    dd[ , names(dd) != "A"]
    
  • To cope with multiple columns (courtesy of @Tyler)

    dd[ ,!(colnames(dd) %in% c("A", "B"))]
    



回答2:


One could use the which() function to identify the column to be eliminated.

dd <- data.frame(A = 1:5, B = 1:5, C=1:5)

dd[, -which(names(dd) == "A")]

or positively

dd[, which(names(dd) != "A")]

However, if there is no column named "A", you would get a data frame with 0 columns and nrow(dd) rows. So it would be good to check for the existence of a column named "A".

if(any(names(dd) == "A")) {
  dd[, which(names(dd) != "A")]
}



回答3:


The subset function already allows this type of syntax, from the examples on the help page:

subset(airquality, Day == 1, select = -Temp)



回答4:


For deselecting multiple columns you can use the dplyr package. As an example:

dd = data.frame(A = 1:3, B = 1:3, C=1:3, D=1:3)

library(dplyr)
newdd <- select(dd, -A,-C)

this is another way besides what @csgillespie suggested.




回答5:


remove A and C

base solution

df <- data.frame(A = 1:3, B = 1:3, C=1:3, D=1:3)

df[,c("A","C")]<-NULL

data.table solution

dt <- data.table(A = 1:3, B = 1:3, C=1:3, D=1:3)

#    A B C D
# 1: 1 1 1 1
# 2: 2 2 2 2
# 3: 3 3 3 3

dt[,c("A","C"):=NULL]

#   B D
#1: 1 1
#2: 2 2
#3: 3 3


来源:https://stackoverflow.com/questions/9805507/deselecting-a-column-by-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!