library(tidyverse)
library(lubridate)
set.seed(1)
df1 = tibble(
ID = rep(1:3, each = 10),
Time = rep(ymd("2013-01-11") + 1:10, 3),
PM25 = sample(300, 30))
df2 = tibble(
ID = c(1,3),
T1 = ymd(c("2013-01-17","2013-01-13")),
T2 = ymd("2013-01-19") + 1:2)
df1
df2
df1 %>% group_nest(ID) %>% right_join(df2,by="ID") %>%
mutate(pm25_m=
pmap_dbl(.[-1],~filter(
..1,between(Time,..2,..3))
|> pull(PM25)|> mean()))