title: How to recode negative values in a set of variables based on if conditional statement in R?
body: <p>R newbie here. I am currently trying to recode a set of variables in R, such that all negative values within them are recoded as positive values.</p>
  
  <p>Please find simulated data below:</p>
  
  <pre><code>df &lt;- data.frame(ID = c(1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009, 1010, 1011),
                   measure1 = rnorm(11, mean = 0, sd = 1),
                   measure2 = rnorm(11, mean = 0, sd = 1),
                   measure3 = rnorm(11, mean = 0, sd = 1),
                   measure4 = rnorm(11, mean = 0, sd = 1))
  </code></pre>
  
  <p>This is the function I've written so far:</p>
  
  <pre><code>df[2:5] &lt;- sapply(df[2:5], function(x) {
    if(x&lt;0) {
    return(x*-1)
  }  else {
     return(x)
      }
    })
  </code></pre>
  
  <p>although I recieve the following errors:</p>
  
  <pre><code>Warning messages:
  1: In if (x &lt; 0) { :
    the condition has length &gt; 1 and only the first element will be used
  2: In if (x &lt; 0) { :
    the condition has length &gt; 1 and only the first element will be used
  3: In if (x &lt; 0) { :
    the condition has length &gt; 1 and only the first element will be used
  4: In if (x &lt; 0) { :
    the condition has length &gt; 1 and only the first element will be used
  </code></pre>
  
  <p>Any ideas on how to make this work?</p>
  
  <p>Thanks!</p>