title: scaling multiple columns in lists using map, map2, mutate_at, summarise_at
body: <p>I am having problems with the <code>scaled_assessment</code> data I am trying to create.</p>
  
  <p>I have some time series data I split up into <code>analysis</code> and <code>assessment</code>. I want to scale the <code>analysis</code> data and use these scaled <code>means</code> and <code>sd</code>'s to apply to the <code>assessment</code> data. I add comments to the code below.</p>
  
  <p>I run into problems tackling the <code>mutate_at</code> function. I want to apply the scale function which takes in the <code>mean</code> and <code>sd</code> from the <code>analysis</code> data and apply it to the <code>assessment</code> data. - For all columns in the <code>assessment</code> data.</p>
  
  <p>Data / code:</p>
  
  <pre><code>    library(rsample)
  set.seed(1131)
  # I create some random data
  ex_data &lt;- data.frame(row = 1:20, some_cat_var = paste("cat"), some_var = rnorm(20), some_other_var = rnorm(20))
  ex_data
  
  # I create the analysis and assessment splits - the analysis data has 10 observations the assess has 1
  rolled_ex_data &lt;- rolling_origin(ex_data,
                                   initial = 10,
                                   assess = 1, 
                                   cumulative = FALSE,
                                   skip = 0)
  
  # My scaling function to apply to the analysis data
  Scale_Me &lt;- function(x){
    (x - mean(x, na.rm = TRUE)) / sd(x, na.rm = TRUE)
  }
  
  # This I believe "works" I collect the mean and sd from the 3rd and 4th column of the data for each split
  scale_values &lt;- map(rolled_ex_data$splits, ~ analysis(.x) %&gt;% 
                        as_tibble(., .name_repair = "universal") %&gt;% 
                        summarise_at(.vars = 3:ncol(.), .funs = c(mean = "mean", sd = "sd")))
  
  # I then apply the scale function to the analysis data (to columns 3 and 4) for each split
  scaled_analysis &lt;- map(rolled_ex_data$splits, ~ analysis(.x) %&gt;% 
                           as_tibble(., .name_repair = "universal") %&gt;% 
                           mutate_at(.vars = 3:ncol(.), .funs = c(Scale_Me = "scale")))
  
  # My problem is here with the mutate_at function
  scaled_assessment &lt;- map2(rolled_ex_data$splits, scale_values, ~ assessment(.x) %&gt;% 
                              as_tibble(., .name_repair = "universal") %&gt;% 
                              mutate_at(.vars = 3:ncol(.), .funs = c(scaled_col = (.vars - .y$mean) / .y$sd)))
  </code></pre>
  
  <h1>EDIT:</h1>
  
  <p>Okay. I have managed to get it working for the two variables using <code>mutate</code>.</p>
  
  <pre><code>scaled_assessment &lt;- map2(rolled_ex_data$splits, scale_values, ~ assessment(.x) %&gt;% 
                              #as_tibble(.x, .name_repair = "universal") %&gt;% 
                              mutate(
                                some_var_scaled = (some_var - .y$some_var_mean) / .y$some_var_sd,
                                some_other_var_scaled = (some_other_var - .y$some_other_var_mean) / .y$some_other_var_sd
                                )
                            )
  </code></pre>
  
  <p>This gets me a list of 10:</p>
  
  <pre><code>scaled_assessment[[1]]
  scaled_assessment[[2]]
  scaled_assessment[[3]]
  
  &gt; scaled_assessment[[1]]
    row some_cat_var  some_var some_other_var some_var_scaled some_other_var_scaled
  1  11          cat -1.350214      -0.569947       -1.603747            -0.2836588
  &gt; scaled_assessment[[1]]
    row some_cat_var  some_var some_other_var some_var_scaled some_other_var_scaled
  1  11          cat -1.350214      -0.569947       -1.603747            -0.2836588
  &gt; scaled_assessment[[2]]
    row some_cat_var some_var some_other_var some_var_scaled some_other_var_scaled
  1  12          cat 2.242594      -1.195205        3.038992            -0.7670828
  &gt; scaled_assessment[[3]]
    row some_cat_var some_var some_other_var some_var_scaled some_other_var_scaled
  1  13          cat 1.781132      0.9764677        1.593273              1.194117
  </code></pre>
  
  <p>I would like to know how to do it using <code>mutate_at</code> because I do not know the number of time series columns I have to scale. Here I use 2 columns <code>some_var</code> and <code>some_other_var</code> but I could have 3 or 4 columns which is why I tried to use <code>.vars = 3:ncol(.)</code>.</p>