1

Example data

d<-data.table(a=c(1,2,3,4,5))

I want this result:

d[a < 3]

How do achieve the same Using my.column?

my.column<-'a'
d[my.column < 3]
#Empty data.table (0 rows) of 1 col: a

Although the following works, I hope there is a better way:

setnames(d,my.column,"my.column")
d[my.column<3,]
setnames(d,'my.column',my.column)

Note that this also does not work

setkey(d,my.column)
David Arenburg
  • 91,361
  • 17
  • 137
  • 196
Harlan Nelson
  • 1,394
  • 1
  • 10
  • 22

1 Answers1

0

As pointed by David in comment you can use

d[get(my.column) < 3]

The other way, which I personally prefer, is to build a call for i instead of getting the variable:

d[eval(as.name(my.column)) < 3]

Not sure about the performance when using in i. In case of using in j arg I believe the eval(as.name( would be marginally faster - depends on amount of columns AFAIK.

jangorecki
  • 16,384
  • 4
  • 79
  • 160