I've included some code showing how to make list columns from data structured like this. Some good references for how to work with list columns can be found in materials by Jenny Bryan and Hadley Wickham .
Does this do what you hoped?
library(tidyverse)
library(lubridate)
# Create fake, example data -----------------------------------------------
# Dates of interest
disease_dates <- seq(from = ymd("2014-01-01"), to = ymd("2014-12-01"), by = "month")
# Climate
clim_data <- tibble(
clim_date = seq(from = ymd("2014-01-01"), to = ymd("2014-12-01"), by = "month"),
precip = rnorm(n = 12, mean = 500, sd = 50),
temp = rnorm(n = 12, mean = 35, sd = 10),
humidity = rnorm(n = 12, mean = 80, sd = 2))
# See what we've made:
disease_dates
#> [1] "2014-01-01" "2014-02-01" "2014-03-01" "2014-04-01" "2014-05-01"
#> [6] "2014-06-01" "2014-07-01" "2014-08-01" "2014-09-01" "2014-10-01"
#> [11] "2014-11-01" "2014-12-01"
head(clim_data)
#> # A tibble: 6 x 4
#> clim_date precip temp humidity
#> <date> <dbl> <dbl> <dbl>
#> 1 2014-01-01 532. 39.6 76.2
#> 2 2014-02-01 502. 17.4 76.2
#> 3 2014-03-01 444. 37.2 79.3
#> 4 2014-04-01 456. 46.1 78.5
#> 5 2014-05-01 555. 39.9 82.0
#> 6 2014-06-01 506. 27.7 78.3
# Data manipulation -------------------------------------------------------
date_groupings <- map(
# For each date in the disease_dates sequence...
.x = disease_dates,
# Do the following with clim_data...
.f = ~ clim_data %>%
# Filter to include only the last 6 mo, including current month
filter(clim_date %in% c(.x:(.x - months(5)))) %>%
# Add the disease_date we're filtering by for reference
mutate(disease_date = .x))
# Preview what we've made:
date_groupings[[7]]
#> # A tibble: 6 x 5
#> clim_date precip temp humidity disease_date
#> <date> <dbl> <dbl> <dbl> <date>
#> 1 2014-02-01 502. 17.4 76.2 2014-07-01
#> 2 2014-03-01 444. 37.2 79.3 2014-07-01
#> 3 2014-04-01 456. 46.1 78.5 2014-07-01
#> 4 2014-05-01 555. 39.9 82.0 2014-07-01
#> 5 2014-06-01 506. 27.7 78.3 2014-07-01
#> 6 2014-07-01 471. 41.1 80.9 2014-07-01
# Go through the list and keep only data frames with 6 months total
clim_as_list <- keep(.x = date_groupings, .p = ~ nrow(.x) == 6) %>%
# Now make a single table from them
reduce(bind_rows) %>%
# For each disease date...
group_by(disease_date) %>%
# Turn its columns into a list
summarize(across(.cols = c(clim_date, precip, temp, humidity), .fns = list))
# Access list for the first date to check:
clim_as_list[1, "precip"] %>%
flatten()
#> $precip
#> [1] 532.1866 502.2564 444.3053 456.4319 554.6295 506.1366
Created on 2021-10-06 by the reprex package (v2.0.0)