0

Given the following code:

library(data.table)
df1<-data.table(a=c(1,1,2,2,1),b=c(1,3,1,1,2),c=c(2,9,8,3,7))
df2<-data.table(a=c(1,2),b=c(1,1))
n<-c("a","b")

I want to select all rows of df1 which have identical rows in df2, considering only the columns given by n.

So, the result should be:

   a b c
1: 1 1 2
2: 2 1 8
3: 2 1 3

How can I do this efficiently? Thanks!

Funkwecker
  • 766
  • 13
  • 22

1 Answers1

1

You could do an inner join:

df1[df2, on = n, nomatch = 0]

   a b c
1: 1 1 2
2: 2 1 8
3: 2 1 3
arg0naut91
  • 14,574
  • 2
  • 17
  • 38