I'm trying to merge two datasets I have.
df1:
day | month | year | lon | lat | month-year |
---|---|---|---|---|---|
3 | 5 | 2009 | 5.7 | 53.9 | May 2009 |
8 | 9 | 2004 | 6.9 | 52.6 | Sep 2004 |
15 | 9 | 2004 | 3.8 | 50.4 | Sep 2004 |
5 | 5 | 2009 | 2.7 | 51.2 | May 2009 |
28 | 7 | 2005 | 14.8 | 62.4 | Jul 2005 |
18 | 9 | 2004 | 5.1 | 52.5 | Sep 2004 |
df2:
nao-value | sign | month-year |
---|---|---|
- 2.1 | Negative | Sep 2004 |
1.3 | Positive | Jul 2005 |
- 1.1 | Negative | May 2009 |
I want to merge this to add the NAO value for each month and year in the occurrence data, meaning i want the NAO value for each specific month repeated for all registrations of that month in the occurrece data.
Problem is I cannot get the NAO values to line up where it should by the occurrence data, its either placed just repetitive and not alligned with the date it should, given as month-year.x and month-year.y ,or it is given back as NA value.
I have tried a few different approaches:
df3 <- merge(df1, df2, by="month-year")
df3 <- merge(cbind(df1, X=rownames(df1)), cbind(df2, variable=rownames(df2)))
df3 <- merge(df1,df2, by ="month-year", all.x = TRUE,all.y=TRUE, sort = FALSE)
df3 <- merge(df1, df2, by=intersect(df1$month-year(df1), df2$month-year(df2)))
But not of those give the result I desire.
Help is highly appreciated