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?
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?
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"))]
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")]
}
The subset
function already allows this type of syntax, from the examples on the help page:
subset(airquality, Day == 1, select = -Temp)
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
solutiondf <- data.frame(A = 1:3, B = 1:3, C=1:3, D=1:3)
df[,c("A","C")]<-NULL
data.table
solutiondt <- 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