Let's say I have a data table
library(data.table)
DT <- data.table(x=c(1,1,0,0),y=c(0,1,2,3))
column_name <- "x"
x y
1: 1 0
2: 1 1
3: 0 2
4: 0 3
And I want to access all the rows where x = 1, but by using column_name.
The desired output should behave like this:
DT[x==1,]
x y
1: 1 0
2: 1 1
but with x
replaced by column_name
in the input.
Note that this problem is similar to but not quite the same as Select subset of columns in data.table R, and the solution there (using with=FALSE) doesn't work here.
Here are all the things I've tried. None of them work.
DT[column_name ==1,]
DT[.column_name ==1,]
DT[.(column_name) ==1,]
DT[..column_name ==1,]
DT[."column_name" ==1,]
DT[,column_name ==1,]
DT[,column_name ==1,with=TRUE]
DT[,column_name ==1,with=FALSE]
DT[,.column_name ==1,with=TRUE]
DT[,.column_name ==1,with=FALSE]
DT[,..column_name ==1,with=TRUE]
DT[,..column_name ==1,with=FALSE]
DT[,."column_name" ==1,with=TRUE]
DT[,.column_name ==1,with=FALSE]
DT[column_name ==1,with=TRUE]
DT[column_name ==1,with=FALSE]
DT[[column_name==1,]]
subset(DT,column_name==1)
I also have options(datatable.WhenJisSymbolThenCallingScope=TRUE)
enabled
There's obviously some kind of lexical trick I'm missing. I've spent several hours looking through vignettes and SO questions to no avail.