I have some data:
library(data.table)
data(mtcars)
setDT(mtcars)
I have some vectors of names of columns I would like to keep:
some_keep <- c('mpg','cyl')
more_keep <- c('disp','hp')
I want to drop-in-place every column except those named in some_keep
and more_keep
.
I know that I can use setdiff
with names
:
mtcars[, ( setdiff(names(mtcars),c(some_keep,more_keep)) ) := NULL] # this works
But this seems not very readable. I know that to select all but these, I can use with=FALSE
:
mtcars[,-c(some_keep,more_keep), with=FALSE] # returns the columns I want to drop
But then this doesn't work:
mtcars[,(-c(some_keep,more_keep)):=NULL] # invalid argument to unary operator
Nor do these:
mtcars[,-c(some_keep,more_keep)] <- NULL # invalid argument to unary operator
mtcars[,-c(some_keep,more_keep), with=FALSE] <- NULL # unused argument (with = FALSE)
Is there a simpler data.table
expression that doesn't require writing the table's name twice?
Please note that this seemingly duplicate question is really asking about selecting (not dropping) all but specified as shown above.