1
votes

I have a question on the way to select programmatically the rows from a data.table based on values from columns.

Let say I have below Data.table

library(data.table)
DT <- data.table(x=rep(c("a","b","c"),each=3), y=c(1,3,6), v=1:9)

Now I want to select rows where y = 3 and v = 2

Normally I can use below code

> DT[y==3& v==2]
   x y v
1: a 3 2

But in my case, such selection criteria is itself a variable, and put in a different DF

> DF = data.frame('1' = c('y', 'v'), '2' = c(3,2)); DF
  X1 X2
1  y  3
2  v  2

In this case, the value of X2 above will change, even number of rows is also variable (i.e. assuming I have a bigger DT with more columns, some additional rows in DF might come based on the generation criteria of DF)

Is there any way to use DF to select rows in DT programmatically?

2

2 Answers

4
votes

Another option using join:

DT[structure(as.list(DF$X2), names=DF$X1), on=as.character(DF$X1)]
1
votes

An option is to eval by pasteing the columns of 'DF' to create an expression

DT[eval(parse(text= paste(DF$X1, DF$X2,  sep="==", collapse=" & ")))]
#   x y v
#1: a 3 2

or we can specify the .SDcols as the 'X1' column, then compare the .SD with 'X2' and Reduce it to a logical vector with &, subset the rows

DT[DT[, Reduce(`&`, Map(`==`, .SD, DF$X2)),.SDcols = as.character(DF$X1)]]
#   x y v
#1: a 3 2