I have two datasets, one dataset is a calendar translation. Meaning, that there are encoded weeks, e.g:
Week | Start | End |
---|---|---|
2678 |
2011-06-18 | 2011-06-24 |
3689 |
2011-06-25 | 2011-07-01 |
8976 |
2011-07-02 | 2011-07-08 |
All dates have "Date" format, "%Y-%m-%d".
Then, I have a dataset with temperatures. The dataset looks like:
Date | Temperature min | Temperature max |
---|---|---|
2011-06-19 |
14 | 23 |
2011-06-20 |
20 | 26 |
2011-06-21 |
15 | 18 |
I want to combine the datasets, and join it this way that I have following result:
Date | Temperature min | Temperature max | Week |
---|---|---|---|
2011-06-19 |
14 | 23 | 2678 |
2011-06-20 |
20 | 26 | 2678 |
2011-06-21 |
15 | 18 | 2678 |
I tried with if statements, however it did not work out. Is there a way to combine datasets this way?
CodePudding user response:
We could use fuzzy_left_join
with the match_fun
argument:
library(tidyverse)
library(fuzzyjoin)
fuzzy_left_join(df2, df1, by = c("Date" = "Start",
"Date" = "End"),
match_fun = list(`>=`, `<=`)
) %>%
select(-c(Start, End))
Date Temperature_min Temperature_max Week
1 2011-06-19 14 23 2678
2 2011-06-20 20 26 2678
3 2011-06-21 15 18 2678