title: Automatically generate new variable names using dplyr mutate
body: <p>I would like to create variable names dynamically while using dplyr; although, I’d be fine with a non-dplyr solution as well.</p>
  
  <p>For Example:</p>
  
  <pre><code>data(iris)
  library(dplyr) 
  
  iris &lt;- iris %&gt;%
    group_by(Species) %&gt;%
    mutate(
      lag_Sepal.Length = lag(Sepal.Length),
      lag_Sepal.Width  = lag(Sepal.Width),
      lag_Petal.Length = lag(Petal.Length)
    ) %&gt;%
    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)
  </code></pre>
  
  <p>But, instead of doing this three times, I want to create 100 of these “lag” variables that take in the name:  lag_original variable name. I’m trying to figure out how to do this without typing the new variable name 100 times, but I’m coming up short.</p>
  
  <p>I’ve looked into <a href="https://stackoverflow.com/questions/26003574/r-dplyr-mutate-use-dynamic-variable-names">this</a> example and <a href="https://stackoverflow.com/questions/35349130/paste-in-column-names-into-dplyrmutate">this</a> example elsewhere on SO. They are similar, but I’m not quite able to piece together the specific solution I need. Any help is appreciated!</p>
  
  <p><strong>Edit</strong><br>
  Thanks to @BenFasoli for the inspiration. I took his answer and tweaked it just a bit to get the solution I needed. 
  I also used <a href="https://blog.rstudio.org/2016/06/27/dplyr-0-5-0/" rel="nofollow noreferrer">This RStudio Blog</a> and <a href="https://stackoverflow.com/questions/27027347/mutate-each-summarise-each-in-dplyr-how-do-i-select-certain-columns-and-give">This SO post</a>. The "lag" in the variable name is trailing instead of leading, but I can live with that.</p>
  
  <p>My final code is posted here in case it’s helpful to anyone else:</p>
  
  <pre><code>lagged &lt;- iris %&gt;%
    group_by(Species) %&gt;%
    mutate_at(
      vars(Sepal.Length:Petal.Length),
      funs("lag" = lag)) %&gt;%
    ungroup
  
  # A tibble: 6 x 8
    Sepal.Length Sepal.Width Petal.Length Petal.Width Species Sepal.Length_lag Sepal.Width_lag
           &lt;dbl&gt;       &lt;dbl&gt;        &lt;dbl&gt;       &lt;dbl&gt;  &lt;fctr&gt;            &lt;dbl&gt;           &lt;dbl&gt;
  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 &lt;dbl&gt;
  </code></pre>