title: Adding multiple lag variables using dplyr and for loops
body: <p>I have time series data that I'm predicting on, so I am creating lag variables to use in my statistical analysis. I'd like a quick way to create multiple variables given specific inputs so that I can easily cross-validate and compare models.</p>
  
  <p>The following is example code that adds 2 lags for 2 different variables (4 total) given a certain category (A, B, C):</p>
  
  <pre><code># Load dplyr
  library(dplyr)
  
  # create day, category, and 2 value vectors
  days = 1:9
  cats = rep(c('A','B','C'),3)
  set.seed = 19
  values1 = round(rnorm(9, 16, 4))
  values2 = round(rnorm(9, 16, 16))
  
  # create data frame
  data = data.frame(days, cats, values1, values2)
  
  # mutate new lag variables 
  LagVal = data %&gt;% arrange(days) %&gt;% group_by(cats) %&gt;% 
    mutate(LagVal1.1 = lag(values1, 1)) %&gt;%
    mutate(LagVal1.2 = lag(values1, 2)) %&gt;%
    mutate(LagVal2.1 = lag(values2, 1)) %&gt;%
    mutate(LagVal2.2 = lag(values2, 2))
  
  LagVal
  
         days   cats values1 values2 LagVal1.1 LagVal1.2 LagVal2.1 LagVal2.2
    &lt;int&gt; &lt;fctr&gt;   &lt;dbl&gt;   &lt;dbl&gt;     &lt;dbl&gt;     &lt;dbl&gt;     &lt;dbl&gt;     &lt;dbl&gt;
  1     1      A      16     -10        NA        NA        NA        NA
  2     2      B      14      24        NA        NA        NA        NA
  3     3      C      16      -6        NA        NA        NA        NA
  4     4      A      12      25        16        NA       -10        NA
  5     5      B      20      14        14        NA        24        NA
  6     6      C      18      -5        16        NA        -6        NA
  7     7      A      21       2        12        16        25       -10
  8     8      B      19       5        20        14        14        24
  9     9      C      18      -3        18        16        -5        -6
  </code></pre>
  
  <p>My problem comes in at the <code># mutate new lag variables</code> step, since I have about a dozen predictor variables that I would potentially want to lag up to 10 times (~13k row dataset), and I don't have the heart to create 120 new variables. </p>
  
  <p>Here is my attempt at writing a function which mutates new variables given the inputs for <code>data</code> (dataset to mutate), <code>variables</code> (the variables you wish to lag), and <code>lags</code> (the number of lags per variable):</p>
  
  <pre><code>MultiMutate = function(data, variables, lags){
    # select the data to be working with
    FuncData = data
    # Loop through desired variables to mutate
    for (i in variables){
      # Loop through number of desired lags
      for (u in 1:lags){
        FuncData = FuncData %&gt;% arrange(days) %&gt;% group_by(cats) %&gt;%
          # Mutate new variable for desired number of lags. Give new variable a name with the lag number appended
          mutate(paste(i, u) = lag(i, u))
      }
    }
    FuncData
  }
  </code></pre>
  
  <p>To be honest I'm just sort of lost on how to get this to work. The ordering of my for-loops and overall logic makes sense, but the way the function takes characters into variables and the overall syntax seems way off. Is there a simple way to fix up this function to get my desired result?</p>
  
  <p><strong>In particular, I'm looking for:</strong></p>
  
  <ol>
  <li><p>A function like <code>MultiMutate(data = data, variables = c(values1, values2), lags = 2)</code> that would create the exact result of <code>LagVal</code> from above.</p></li>
  <li><p>Dynamically naming the variables based on the variable and their lag. I.e. value1.1, value1.2, value2.1, value2.2, etc.</p></li>
  </ol>
  
  <p>Thank you in advance and let me know if you need additional information. If there's a simpler way to get what I'm looking for, then I am all ears. </p>