How can I do a "temporal join" between to tibbles?
library(tidyverse)
library(lubridate)
ymd(c("2014-12-20")) %within% interval(ymd("2014-12-19"),ymd("2014-12-25"))
just works
but:
holidays <- tibble(kind=c("winter","early","spring","summer","autumn"),
start=c("2021-12-18","2022-02-26","2022-04-09","2022-07-02","2022-10-01"),
end=c("2022-01-01","2022-03-12","2022-04-23","2022-08-13","2022-10-15"))
holidays <- holidays %>%
mutate (during=interval(ymd(start), ymd(end)))
test <- tibble(incident_name=c("birth","nothing","wedding","sick"), incident_date=c("2022-02-01","2022-06-29","2022-07-29","2022-08-30")) %>%
mutate (incident_date=ymd(incident_date))
test %>%
filter(incident_date %within% as.vector(holidays[["during"]])) %>%
mutate(new_field=holidays[["incident_name"]])
does not work. Even without the mutate (I am not sure if I get a vector or a matrix out of this) it seems the %within% does not work this way. Can someone please explain the syntax?
One goal is to get the kind of holidays for every incident_name (or NA, if not within this holiday). But I would just be happy to get a TRUE or FALSE for every combination, so I can do a mutate on that basis.
CodePudding user response:
According to %within%
,
a - An interval or date-time object.
b - Either an interval vector, or a list of intervals.
We can use rowwise
as the length of 'b' is different from 'a'
library(dplyr)
library(lubridate)
test %>%
rowwise %>%
filter(any(incident_date %within% holidays$during)) %>%
ungroup
-output
# A tibble: 1 × 2
incident_name incident_date
<chr> <date>
1 wedding 2022-07-29