Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to use mutate within a for loop after using a group_by?

Sample dataframe is as follows:

dat = data.frame(a = c(rep(1,5),rep(2,4),rep(3,7)), b = c(1:16)) 

for (i in 1:21){
dat %<>% group_by(a) %>% 
  mutate(paste('lag_',i,,sep = '') = lag(b, n = i))
}

However, the error popped up:

Error: unexpected '=' in:
"  dat%<>% group_by(a) %>% 
    mutate(paste('lag_',i,,sep = '') ="

Is there any way to name the variable by using mutate after group_by within forloop?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

One potential solution:

library(tidyverse)

dat = data.frame(a = c(rep(1,5),rep(2,4),rep(3,7)), b = c(1:16)) 

for (i in 1:21){
  dat %<>% group_by(a) %>% 
    mutate(!!paste('lag_',i,sep = '') := lag(b, n = i))
}
dat
#> # A tibble: 16 × 23
#> # Groups:   a [3]
#>        a     b lag_1 lag_2 lag_3 lag_4 lag_5 lag_6 lag_7 lag_8 lag_9 lag_10
#>    <dbl> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int>  <int>
#>  1     1     1    NA    NA    NA    NA    NA    NA    NA    NA    NA     NA
#>  2     1     2     1    NA    NA    NA    NA    NA    NA    NA    NA     NA
#>  3     1     3     2     1    NA    NA    NA    NA    NA    NA    NA     NA
#>  4     1     4     3     2     1    NA    NA    NA    NA    NA    NA     NA
#>  5     1     5     4     3     2     1    NA    NA    NA    NA    NA     NA
#>  6     2     6    NA    NA    NA    NA    NA    NA    NA    NA    NA     NA
#>  7     2     7     6    NA    NA    NA    NA    NA    NA    NA    NA     NA
#>  8     2     8     7     6    NA    NA    NA    NA    NA    NA    NA     NA
#>  9     2     9     8     7     6    NA    NA    NA    NA    NA    NA     NA
#> 10     3    10    NA    NA    NA    NA    NA    NA    NA    NA    NA     NA
#> 11     3    11    10    NA    NA    NA    NA    NA    NA    NA    NA     NA
#> 12     3    12    11    10    NA    NA    NA    NA    NA    NA    NA     NA
#> 13     3    13    12    11    10    NA    NA    NA    NA    NA    NA     NA
#> 14     3    14    13    12    11    10    NA    NA    NA    NA    NA     NA
#> 15     3    15    14    13    12    11    10    NA    NA    NA    NA     NA
#> 16     3    16    15    14    13    12    11    10    NA    NA    NA     NA
#> # ℹ 11 more variables: lag_11 <int>, lag_12 <int>, lag_13 <int>, lag_14 <int>,
#> #   lag_15 <int>, lag_16 <int>, lag_17 <int>, lag_18 <int>, lag_19 <int>,
#> #   lag_20 <int>, lag_21 <int>

Created on 2023-10-10 with reprex v2.0.2

For more details, see https://adv-r.hadley.nz/quasiquotation.html?q=:=#tidy-dots

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading