Pipe result from map_dfr to a subsequent map_dfr to apply custom fuction to groups of data

Solution 1:

Is this the expected output?

library(tidyverse)
library(lubridate)

group_split(df, dim1, dim2) %>%
  map_dfr(~ .x %>% AddLags(1:2, "value", date))
#> # A tibble: 12 × 2
#>    value_lag_01 value_lag_02
#>           <int>        <int>
#>  1           NA           NA
#>  2            1           NA
#>  3            2            1
#>  4           NA           NA
#>  5            4           NA
#>  6            5            4
#>  7           NA           NA
#>  8            7           NA
#>  9            8            7
#> 10           NA           NA
#> 11           10           NA
#> 12           11           10

Data:

# dummy dataset
df <- data.frame(
  date = seq(today(), length.out = 12, by = "month"),
  dim1 = c("a", "a", "a", "b", "b", "b", "c", "c", "c", "d", "d", "d"),
  dim2 = c(1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2),
  value = 1:12
)

# function to apply
AddLags <- function(df, lags_vector, target_col, date_col) {
  temp_lags <- map_dfc(
    lags_vector,
    ~ df %>%
      arrange({{ date_col }}) %>%
      transmute(
        across(contains(target_col), lag, .x, .names = '{col}_lag_{ifelse(.x<10,paste0("0",.x),.x)}')
      )
  )
  return(temp_lags)
}

Created on 2022-01-13 by the reprex package (v2.0.1)