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