0

I would like to write a loop to export R data from multiple years to separate excel files. It is not as straightforward as I would like it to be. Any suggestion on how I can go about this?

So my tedious R code looks like this:

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")

I would love to transform it to something like this:

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")

}
Roland
  • 127,288
  • 10
  • 191
  • 288
May
  • 41
  • 1
  • 7
  • Are the date from all of the years in the same R data frame? Please provide some sample data if you can. – ccapizzano Jun 26 '14 at 19:46
  • So originally the data from all the years are in the same data frame. But my previous codes split things into separate data frames based on different years. So scoreY1_2013 is now a different data frame from scoreY1_2012. I hope I answer your question as I am not fully understanding what you are after probably because I am so new to R. – May Jun 26 '14 at 20:03
  • Nope, you answered my question just fine. For future questions, look into creating a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) to make things easier. – ccapizzano Jun 26 '14 at 20:04
  • Hi ccapizzano, thanks for the link on how to post parts of the data. Since I have no clue on how to post sample data effectively, I will definitely look into the link. – May Jun 26 '14 at 20:29

1 Answers1

0

This would be a lot simpler if you stored all your various variables in data frames or lists (or a list of data frames). but you can try something like:

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))
}
Greg Snow
  • 48,497
  • 6
  • 83
  • 110
  • Yes this totally WORKS!!!!! Thanks!! So if I stack all the variables from different years on top of each other(so scoreY1_2013 and scoreY1_2012 and scoreY1_2011 on top of each other for example) and save it as one data frame, how would this simplify my loop problem? – May Jun 26 '14 at 20:19
  • @user3780706, stacking is one option if all the scores were in one column and eff in another and you have another column named year, then you could `write.csv( mydf[ mfdf$year==i, c('score','eff') ], `, or even if you had a list with each of your variables in it, then you could replace the call to `get` with `mylist[[sprintf("scoreY1_%d",i)]]`. I would suggest the list of data frames with each data frame being a year, then you can just loop through that list and write out the whole data frame). – Greg Snow Jun 26 '14 at 20:25