title: R: How to recode multiple variables at once
body: <p>I have several variables in my dataset that need to be recoded in exactly the same way, and several other variables that need to be recoded in a different way. I tried writing a function to help me with this, but I'm having trouble.</p>
  
  <pre><code>library(dplyr)
  recode_liberalSupport = function(arg1){
    arg1 = recode(arg1, "1=-1;2=1;else=NA")
    return(arg1)
  }
  
  liberals = c(df$var1, df$var4, df$var8)
  for(i in unique(liberals)){
    paste(df$liberals[i] &lt;- sapply(liberals, FUN = recode_liberalSupport))
  }
  </code></pre>
  
  <p>R studio works on this for about 5 minutes then gives me this error message:</p>
  
  <pre><code>Error in `$&lt;-.data.frame`(`*tmp*`, liberals, value = c(NA_real_, NA_real_,  : 
    replacement has 9 rows, data has 64600
  In addition: Warning messages:
  1: Unknown or uninitialised column: 'liberals'. 
  2: In df$liberals[i] &lt;- sapply(liberals, FUN = recode_liberalSupport) :
    number of items to replace is not a multiple of replacement length
  </code></pre>
  
  <p>Any help would be really appreciated! Thank you</p>