[R] use variable in for loop to name output files

john-usace john.r.kucharski at usace.army.mil
Mon Dec 10 22:03:29 CET 2012


Hi,

This question should be simple to answer. I am a new R user. 

I have a data.frame called appended. I would like to break it into 7 smaller
datasets based on the value of a categorical variable dp (which has values
1:7). I would like to name the smaller datasets set1, set2, set3,....,set7.
I don't know how to refer to the variable in the for loop, when naming the
output datasets. In STATA (which I am much more familiar with) each i in the
foreach loop would be refered to as `i'. This is the code I've included
below. I've also tried set[[i]] and set[i] neither works.

for (i in 1:7) {
	set`i' = appended[which(appended$dp==i & appended$sampled==0), ]
	write.table(set`i', file = "output\\set`i'.csv", sep = ",", row.name=F)
	}

I'm assuming I just need to replace `' with something else but I can figure
out what that something else is.



--
View this message in context: http://r.789695.n4.nabble.com/use-variable-in-for-loop-to-name-output-files-tp4652711.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list