Deselecting a column by name

2019-02-01 20:45发布

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?

5条回答
Rolldiameter
2楼-- · 2019-02-01 21:26

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.

查看更多
成全新的幸福
3楼-- · 2019-02-01 21:36

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"))]
    
查看更多
Evening l夕情丶
4楼-- · 2019-02-01 21:44

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

subset(airquality, Day == 1, select = -Temp)
查看更多
趁早两清
5楼-- · 2019-02-01 21:48

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")]
}
查看更多
戒情不戒烟
6楼-- · 2019-02-01 21:48

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
查看更多
登录 后发表回答