0
votes

I have 2 dataframes.

I would like to subset Df1 to return a new dataframe where the id and month are a match from the lookup dataframe: lookupDf.

Those matching rows would be result in Df3.

For example:

Df1

id    month   x
A    20      10
B    20      11
C    20      12
D    20      13
E    20      14
A    21      15
B    21      16
C    21      17
D    21      18
E    21      19


lookupDf

id    month  
A    20     
B    20   
C    20  
E    20    
C    21     
D    21      

Df3 would be a subset of Df1

Df3

id    month   x
A    20      10
B    20      11
C    20      12
E    20      14
C    21      17
D    21      18
1
Simple merge: Df3 = merge(Df1, lookupDf) - Parfait

1 Answers

2
votes

Using tidyverse tools, look at the various join functions in dplyr.

Here you want to keep rows from df1 that match in lookupDf, so you want to use a semi_join, which does not duplicate rows in df1 if they match to more than one row in lookupDf,.

Df3 <- dplyr::semi_join(Df1, lookupDf)