Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using another data table to condition on columns in a primary data table r

Suppose I have two data tables, and I want to use the second one, which contains a row with some column values, to condition the first one.

Specifically, I want to use d2 to select rows where its variables are less than or equal to the values.

 d1 = data.table('d'=1,'v1'=1:10, 'v2'=1:10)
 d2 = data.table('v1'=5, 'v2'=5)

So I would want the output to be

   d v1 v2
1: 1  1  1
2: 1  2  2
3: 1  3  3
4: 1  4  4
5: 1  5  5

But I want to do this without referencing specific names unless it's in a very general way, e.g. names(d2).

like image 933
wolfsatthedoor Avatar asked May 07 '18 21:05

wolfsatthedoor


People also ask

How do I concatenate two data tables in R?

To join two data frames (datasets) vertically, use the rbind function. The two data frames must have the same variables, but they do not have to be in the same order. If data frameA has variables that data frameB does not, then either: Delete the extra variables in data frameA or.

What does setDT do in R?

setDT converts lists (both named and unnamed) and data. frames to data. tables by reference. This feature was requested on Stackoverflow.


1 Answers

You could do it with a bit of text manipulation and a join:

d2[d1, on=sprintf("%1$s>=%1$s", names(d2)), nomatch=0]

#   v1 v2 d
#1:  1  1 1
#2:  2  2 1
#3:  3  3 1
#4:  4  4 1
#5:  5  5 1

It works because the sprintf expands to:

sprintf("%1$s>=%1$s", names(d2))
#[1] "v1>=v1" "v2>=v2"
like image 139
thelatemail Avatar answered Oct 26 '22 22:10

thelatemail