I would like to dynamically create variable names when using dplyr; though, I'd be fine with a non-dplyr solution.
Example:
data(iris) library(dplyr) iris <- iris %>% group_by(Species) %>% mutate( lag_Sepal.Length = lag(Sepal.Length), lag_Sepal.Width = lag(Sepal.Width), lag_Petal.Length = lag(Petal.Length) ) %>% ungroup head(iris) Sepal.Length Sepal.Width Petal.Length Petal.Width Species lag_Sepal.Length lag_Sepal.Width (dbl) (dbl) (dbl) (dbl) (fctr) (dbl) (dbl) 1 5.1 3.5 1.4 0.2 setosa NA NA 2 4.9 3.0 1.4 0.2 setosa 5.1 3.5 3 4.7 3.2 1.3 0.2 setosa 4.9 3.0 4 4.6 3.1 1.5 0.2 setosa 4.7 3.2 5 5.0 3.6 1.4 0.2 setosa 4.6 3.1 6 5.4 3.9 1.7 0.4 setosa 5.0 3.6 Variables not shown: lag_Petal.Length (dbl)
But instead, three times, I want to create 100 of these lagging variables that take a name: lag_original is the name of the variable. I am trying to figure out how to do this without typing a new variable name 100 times, but Im getting closer.
Ive reviewed this example and this example elsewhere on SO. They are similar, but Im not quite able to put together the specific solution that I need. Any help is appreciated!
Edit
Thanks to @BenFasoli for inspiration. I took his answer and changed it a bit to get the solution I needed. I also used This RStudio Blog and This SO Post. . The βdelayβ in the variable name ends instead of the lead, but I can live with it.
My latest code is posted here if it is useful to anyone else:
lagged <- iris %>% group_by(Species) %>% mutate_at( vars(Sepal.Length:Petal.Length), funs("lag" = lag)) %>% ungroup # A tibble: 6 x 8 Sepal.Length Sepal.Width Petal.Length Petal.Width Species Sepal.Length_lag Sepal.Width_lag <dbl> <dbl> <dbl> <dbl> <fctr> <dbl> <dbl> 1 5.1 3.5 1.4 0.2 setosa NA NA 2 4.9 3.0 1.4 0.2 setosa 5.1 3.5 3 4.7 3.2 1.3 0.2 setosa 4.9 3.0 4 4.6 3.1 1.5 0.2 setosa 4.7 3.2 5 5.0 3.6 1.4 0.2 setosa 4.6 3.1 6 5.4 3.9 1.7 0.4 setosa 5.0 3.6 # ... with 1 more variables: Petal.Length_lag <dbl>
source share