0

This problem has been annoying me for the past couple of hours. I need to merge two data frames together which have a common name variable but differing year variables. I need to conditionally merge based on the year of df1 being between two dates in df2.

The data looks as follows:

Name   Year   X
Rob    1990   Red
Bob    1991   Blue
Alice  1992   Yellow
Alice  1994   Blue

And

Name   startDate  endDate Y
Rob    1989       1994    xxx 
Bob    1982       1993    yyy
Alice  1983       1990    xxx
Alice  1990       1995    hhhh
Alice  2000       2001    gggg

I'm interested in getting the Y variable from df2 into df1 like so:

Name   Year   X      Y
Rob    1990   Red    xxx
Bob    1991   Blue   yyy 
Alice  1992   Yellow hhhh
Alice  1994   Blue   hhhh

Merging by speakers, of course, does not fit the data and my attempts at creating a for loop with an if statement are falling flat.

If anyone can help, I'd be extremely grateful.

nj95
  • 133
  • 7

0 Answers0