I have a data.table
structure like so (except mine is really huge):
dt <- data.table(x=1:5, y=3:7, key='x')
I want to look up rows in that structure by another variable whose name is x
(notice - the same as the name of the key of dt
):
x <- 3:4
dt2 <- dt[ J(x) ]
This doesn't work, because the lookup sees the column name first, and the local variable is obscured:
dt2
# x y
# 1: 1 3
# 2: 2 4
# 3: 3 5
# 4: 4 6
# 5: 5 7
I thought about the with
argument for [.data.table
, but that only applies to the j
argument, not the i
argument.
Is there something similar for the i
argument?
If not, such a thing would be handy whenever I'm using a local variable and I don't know the complete list of column names in dt
, to avoid conflicts.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…