Loop over string variables in R

后端 未结 4 1095
囚心锁ツ
囚心锁ツ 2021-01-31 20:51

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:

4条回答
  •  无人共我
    2021-01-31 21:40

    If your main issue is the need to type eval(parse(text=i)) instead of ``i'`, you could create a simpler-to-use functions for evaluating expressions from strings:

    e = function(expr) eval(parse(text=expr))
    

    Then the R example could be simplified to:

    clist <- c("nominalprice", "realprice")
    for (i in clist) {
      png(paste("c:/TimePlot-", i, ".png", sep=""))
      plot(time, e(i))
      dev.off() 
    }
    

提交回复
热议问题