title: How to gather then mutate a new column then spread to wide format again
body: <p>Using tidyr/dplyr, I have some factor columns which I'd like to Z-score, and then mutate an average Z-score, whilst retaining the original data for reference.</p>
  
  <p>I'd like to avoid using a for loop in tidyr/dplyr, thus I'm gathering my data and performing my calculation (Z-score) on a single column. However, I'm struggling with restoring the wide format.</p>
  
  <p>Here is a MWE:</p>
  
  <pre><code>library(dplyr)
  library(tidyr)
  
  # Original Data
  dfData &lt;- data.frame(
    Name = c("Steve","Jwan","Ashley"),
    A = c(10,20,12),
    B = c(0.2,0.3,0.5)
  ) %&gt;% tbl_df() 
  
  # Gather to Z-score
  dfLong &lt;- dfData %&gt;% gather("Factor","Value",A:B) %&gt;% 
    mutate(FactorZ = paste0("Z_",Factor)) %&gt;% 
    group_by(Factor) %&gt;% 
    mutate(ValueZ = (Value - mean(Value,na.rm = TRUE))/sd(Value,na.rm = TRUE))
  
  # Now go wide to do some mutations (eg Z)Avg = (Z_A + Z_B)/2)
  
  # This does not work
  dfWide &lt;- dfLong %&gt;% 
    spread(Factor,Value) %&gt;%
    spread(FactorZ,ValueZ)%&gt;% 
    mutate(Z_Avg = (Z_A+Z_B)/2)
  
  
  # This is the desired result
  dfDesired &lt;- dfData %&gt;% mutate(Z_A = (A - mean(A,na.rm = TRUE))/sd(A,na.rm = TRUE)) %&gt;% mutate(Z_B = (B - mean(B,na.rm = TRUE))/sd(B,na.rm = TRUE)) %&gt;% 
                      mutate(Z_Avg = (Z_A+Z_B)/2)
  </code></pre>
  
  <p>Thanks for any help/input!</p>