I’m trying to merge two data frames on a unique id and year. In SQL language I’m trying to do a left outer join, so in merge that is all.x=TRUE. Some elements of the y dataframe dont have all of the values (unique id,year combinations) in the x DF. In the case of a missing match I want to merge the row from the y data frame that has the same unique id as in the x data frame, but using the first year that I have prior to the missing one. Any suggestions on how to approach this merge? Thanks a lot!
Edit Wanted to make it more concrete
Dataframe x:
Id year var1
1 2010 100
1 2011 105
1 2012 110
2 2010 100
2 2011 105
2 2012 106
Dataframe y:
Id year var2 var3
1 2010 5 7
1 2011 10 8
2 2010 9 6
Desired merge:
Id year var1 var2 var3
1 2010 100 5 7
1 2011 105 10 8
1 2012 110 10 8
2 2010 100 9 6
2 2011 105 9 6
2 2012 106 9 6
I’d do this in two steps:
Then use
na.locffrom thezoopackage:and this can be coerced to a data.frame easily enough.