我怎样才能在“临时连接”之间进行“临时连接”呢?
library(tidyverse)
library(lubridate)
ymd(c("2014-12-20")) %within% interval(ymd("2014-12-19"),ymd("2014-12-25"))只起作用
但是:
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"]])不管用。即使没有变异(我不确定是否从中得到向量或矩阵),%中的%似乎也不是这样工作的。谁能解释一下语法吗?
一个目标是为每一个incident_name (或者NA,如果不是在这个假期内)提供假期。但我会很高兴得到一个正确或错误的每一个组合,所以我可以做一个变异的基础上。
发布于 2022-08-29 20:04:00
据%within%称,
a-间隔或日期时间对象.B-一个区间向量,或一个区间列表.
我们可以使用rowwise,因为'b‘的长度不同于'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 https://stackoverflow.com/questions/73534020
复制相似问题