I want to subset a data.table
using a variable which has the same name as the column which leeds to some problems:
dt <- data.table(a=sample(c('a', 'b', 'c'), 20, replace=TRUE),
b=sample(c('a', 'b', 'c'), 20, replace=TRUE),
c=sample(20), key=c('a', 'b'))
evn <- environment()
a <- 'b'
dt[a == a]
#Expected Result
dt[a == 'b']
I came across this possible solution:
env <- environment()
dt[a == get('a',env)]
But it is as unhandy as:
this.a = a
dt[a == this.a]
So is there another elegant solution?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…