Hi,
I am trying to generate multiple lags in my dataset to see if certain days correlate with each other. I am trying to use dplyr and setNames for this based on a post by Dr Simon Jackson on Github
My code is below and i am getting the following error based on looking for 6 lags
Error in setNames(paste("apply_lags(., ", lags, ")")) :
argument "nm" is missing, with no default
Does anyone know where i am going wrong?
# Here we create many lag functions using SetNames
apply_lags <- function(k, mydf) {
label = glue::glue("lag_{k}_day")
mydf %>%
mutate("lag_{{k}}_day" := lag(shps, n = k))
}
# Create a dataframe
x = seq(2,20,2) %>%
enframe()
# We set the number of lags we are interested in to 3
lags <- seq(1:3)
# ERROR HERE
lag_functions <- setNames(paste("apply_lags(., ", lags, ")"))
# Apply this to all the functions we have created
x %>%
mutate_at(vars(lags), funs_(lag_functions))