Loop over string variables in R

后端 未结 4 1026
囚心锁ツ
囚心锁ツ 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:29

    As other people have intimated, this would be easier if you had a dataframe with columns named nominalprice and realprice. If you do not, you could always use get. You shouldn't need parse at all here.

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

提交回复
热议问题