[R] use variable in for loop to name output files
Rui Barradas
ruipbarradas at sapo.pt
Mon Dec 10 22:51:52 CET 2012
Hello,
Try the following.
set <- list()
for (i in 1:7) {
set[[i]] <- appended[which(appended$dp == i & appended$sampled == 0), ]
fl <- paste0("output/set", i, ".csv")
write.table(set[[i]], file = fl, sep = ",", row.name=F)
}
Hope this helps,
Rui Barradas
Em 10-12-2012 21:03, john-usace escreveu:
> 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.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list