我想写一个循环来将多年的R数据导出到单独的excel文件中。它并不像我希望的那样简单明了。关于如何解决这个问题的任何建议?
所以我繁琐的R代码看起来像这样:
id_all = data.frame(temp[c(1:4)])
result_all = data.frame(id_all,scoreY1_2013,eff.bcY1_2013, scoreY2_2013,eff.bcY2_2013, x2013,y2_2013)
write.csv(result_all, file = "R/Results/test_2013.csv")
result_all = data.frame(id_all,scoreY1_2012,eff.bcY1_2012, scoreY2_2012,eff.bcY2_2012, x2012,y2_2012)
write.csv(result_all, file = "R/Results/test_2012.csv")
result_all = data.frame(id_all,scoreY1_2011,eff.bcY1_2011, scoreY2_2011,eff.bcY2_2011, x2011,y2_2011)
write.csv(result_all, file = "R/Results/test_2011.csv")
我很想把它变成这样的东西:
For (i = 2011:2013) {
id_all = data.frame(temp[c(1:4)])
result_all = data.frame(id_all,scoreY1_i,eff.bcY1_i, scoreY2_i,eff.bcY2_i, xi,y2_i)
write.csv(result_all, file = "R/Results/test_i.csv")
}
答案 0 :(得分:0)
如果将所有各种变量存储在数据框或列表(或数据框列表)中,这将更加简单。但你可以尝试类似的东西:
for(i in 2011:2013) {
result_all <- data.frame(id_all, get( sprintf("scoreY1_%d", i)),
get(sprintf("eff.bcY1_%d",i)))
write.csv(result_all, file=sprintf("R/Results/test_%d.csv",i))
}