R: looping through column names
<p>I am a Stata user trying to switch to R and having the usual beginner's struggle. I have been trying (and failing) to do a loop for a few days and I now surrender. 
What I want to do (in a loop):</p>

<ul>
<li><p>start from a list of variable names</p></li>
<li><p>create a new variable</p></li>
<li><p>recode that new variable(s) based on the value of existing variables</p></li>
<li><p>possibly do so using the dplyr syntax, but this is not essential,
only for consistency with the rest of my code.</p></li>
</ul>

<p>Here is a stylised example of what I am trying to do. In my actual data, the x.x and x.y variables originate from the join function applied to 2 existing data frames. </p>

<pre><code>N &lt;- 1000
  df  &lt;- data.frame(x1 = rnorm(N),
x2.x = rnorm(N)+2,x2.y = rnorm(N)-2,
x3.x = rnorm(N)+3,x3.y = rnorm(N)-3)

varlist &lt;- c("x2","x3")
lapply(varlist, function(x) {
   df &lt;- df %&gt;% mutate(x = ifelse(x1 &lt; 0, paste0(x,".y"),paste0(x,".x")) # generate varialble "x" values from existing x.x and x.y
  })
</code></pre>

<p>When I run the lapply part of the code I get the error message </p>

<blockquote>
  <p>Error: unexpected '}' in:
  "   df &lt;- df %>% mutate(x = ifelse(x1 &lt; 0, paste0(x,".y"),paste0(x,".x")) # generate varialble "x" values from existing x.x and x.y
    }"</p>
</blockquote>

<p>even though it should be expected... I am sure there a number of mistakes in my code, and that's partly because I am used to macros in Stata for which there is no direct equivalent in R. Anyway, if you can point me in the right direction it would be fantastic!</p>