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

5 Answers

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"))] 
0

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")] } 
0

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

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

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.

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