title: Loop over string variables in R
body: <p>When programming in Stata I often find myself using the loop index in the programming. For example, I'll loop over a list of the variables nominalprice and realprice:  </p>
  
  <pre><code>local list = "nominalprice realprice"
  foreach i of local list {
    summarize `i'
    twoway (scatter `i' time)
    graph export "C:\TimePlot-`i'.png"
  }
  </code></pre>
  
  <p>This will plot the time series of nominal and real prices and export one graph called TimePlot-nominalprice.png and another called TimePlot-realprice.png.  </p>
  
  <p>In R the method I've come up with to do the same thing would be:  </p>
  
  <pre><code>clist &lt;- c("nominalprice", "realprice")
  for (i in clist) {
    e &lt;- paste("png(\"c:/TimePlot-",i,".png\")", sep="")
    eval(parse(text=e))
    plot(time, eval(parse(text=i)))
    dev.off() 
  }
  </code></pre>
  
  <p>This R code looks unintuitive and messy to me and I haven't found a good way to do this sort of thing in R yet. Maybe I'm just not thinking about the problem the right way? Can you suggest a better way to loop using strings? </p>