1
votes

I am using data.table and I want to filter a data.table within a function where I am passing the name of the column as a character vector.

For a simple reproducible example let's take the mtcars dataset of base R.

I can write using data.table syntax:

mtcars[am == 1, .N ]

But what if the name of the variable of interest -- i.e. am -- is stored as a character vector, i.e. "am"?

Your advice will be appreciated.

2
DT = data.table(mtcars); vname = "am"; DT[.(1), on=vname, .N] is what I'd do. - Frank

2 Answers

2
votes

One option is to use get (?get search object by name):

mtcars[get('am') == 1, .N]
# [1] 13
0
votes

Another option is to specify it in the .SDcols

mtcars[, sum(.SD==1) ,.SDcols = 'am']
#[1] 13

We can also include multiple variables

mtcars[, sum(Reduce(`&`, lapply(.SD, `==`, 1))), .SDcols = c('am', 'carb')]
#[1] 4