title: What is the shortest and cleanest way to recode multiple variables in a dataframe using R?
body: <p>So I'm working in the field of social science and what I often have to do is to manipulate multiple variables to change values. More often than not this means to reverse a scale. I've been working with <em>SPSS</em> for a long time and the syntax there is quite simple. To change the values of multiple variables you write:</p>
  
  <p><code>RECODE var1 var2 var3 (1=5) (2=4) 4=2) (5=1) (ELSE=COPY).</code></p>
  
  <p>To write the new codes in new variables you add <code>into newvar1 newvar1 newvar3.</code> at the end. In the brackets you can use things like <code>hi</code>, <code>lo</code>, <code>1 to 4</code> and so on.</p>
  
  <p>Now I'm working my way into <em>R</em> and I'm struggeling to find the best way to do similar workflows. I found the following solutions, but can't get a short nice way:</p>
  
  <pre><code>## Packages -----
  library(dplyr)
  library(car)
  
  ## Data -----
  tib &lt;- tibble(v1 = 1:4, 
                v2 = 1:4,
                v3 = sample(1:5, 4, replace = FALSE))
  
  vars &lt;- c("v1", "v2", "v3")
  
  </code></pre>
  
  <p>The base way:</p>
  
  <pre><code>tib$v2_rec &lt;- NA
  tib$v2_rec[tib$v2 == 1] &lt;- 5 #1
  tib$v2_rec[tib$v2 == 2] &lt;- 4 #2
  tib$v2_rec[tib$v2 == 3] &lt;- 3 #3
  tib$v2_rec[tib$v2 == 4] &lt;- 2 #4
  tib$v2_rec[tib$v2 == 5] &lt;- 1 #5
  # I'm forced to create a new variable here, otherwise #4 and #5 overwrite #1 and #2.
  # Therefore I won't even bother to try to loop trough multiple variables.
  </code></pre>
  
  <p>recode() from the package car:</p>
  
  <pre><code>tib$v1 &lt;- recode(tib$v1, "1=5; 2=4; 4=2; 5=1")
  # This is nice, understandable and short
  # To handle multiple variables the following solutions won't work, because the reload functions seems not to be able to iterate through lists:
  
  tib[vars] &lt;- recode(tib[vars], "1=5; 2=4; 4=2; 5=1")
  tib[1:3] &lt;- recode(tib[1:3], "1=5; 2=4; 4=2; 5=1")
  
  # I'd be forced to loop:
  
  for (i in vars) {
    tib[[i]] &lt;- recode(tib[[i]], "1=5; 2=4; 4=2; 5=1")
  }
  </code></pre>
  
  <p>I'm pretty happy with that but I was wondering if there's a function that would do the work of looping for me. I'm realy struggling with the dplyer functions at the moment and I'm not happy how I can't figure things out intuitively...</p>
  
  <p>I tried mutate:</p>
  
  <pre><code>#I get it for a single case and for multiple cases i got to a solution in combination with the recode() function:
  
  tib &lt;- tib %&gt;%
    mutate_at(vars(v1:v3), 
              function(x) recode(x, "1=5; 2=4; 4=2; 5=1"))
  
  </code></pre>
  
  <p>Is this the best way to do this? Just to be clear, I saw some other solutions using case_when(), replace() or mapvalues() but I find the solution above better, because I like to see what value gets recoded to what value in one glimps.</p>
  
  <p>I got a little into the apply() function and could not even recode one variable with it. I'm sure I'll get a grip on that as well soon, but at the moment I'm just a little frustrated how long I'm looking around for workflows that took me one line in SPSS. If you know any shorter and cleaner solution than the one above using the apply() function I would be greatful!</p>
  
  <p>I'm happy with R and it's possibilities but right now I need a hint in the right direction to keep me going! Thank you in advance!</p>