0

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
jjordan
  • 42
  • 5
  • 2
    Simple merge: `Df3 = merge(Df1, lookupDf)` – Parfait Jan 29 '18 at 20:08
  • Possible duplicate of [Merge two data frames while keeping the original row order](https://stackoverflow.com/questions/17878048/merge-two-data-frames-while-keeping-the-original-row-order) – Onyambu Jan 29 '18 at 20:11

1 Answers1

2

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)
Calum You
  • 14,687
  • 4
  • 23
  • 42