tags:

views:

120

answers:

2

Hello!

In R, I would like to create a loop which takes the first 3000 columns of my data frame and writes them into one file, the next 3000 columns into another file, and so on and so forth until all columns have been divided as such. What would be the best way to do this? I understand there are the isplit and iterators functions available now via CRAN, but I am really unsure how to go about this. Any suggestions please?

Thanks for the help in advance!

+9  A: 

You could try something like:

library(plyr)
max.col <- ncol(x)
l_ply(seq(1, max.col, by=3000), function(i) 
    write.table(x[,i:min(i+2999, max.col)], file=paste("i", i, sep="-"))
)
Shane
Thanks Shane for the reply back! I tried this out, the result is a single file titled "i" with 2456 columns - which I am assuming is the last iteration of the loop? My followup question is how can I now modify this code such that I can give individual names to each of the files that are outputted? For example "i-1", "i-2", "i-3"Thank you so much for all of your help, it has been very useful! I am an absolute beginner with programing/coding.
CCA
Sorry...my mistake. I updated it to reflect your output naming convention.
Shane
CCA
@CCA: I recommend digging into the documentation and examples. For instance, look at `help(write.table)`. Yes: you can specify a full path in the paste() function call above. You can do whatever you want there: try experimenting with `paste` too: help(paste). Incidentally, please accept this as the answer when you are satisfied so that others will know the question is solved.
Shane
will do..thank you!
CCA
+1  A: 

Not sure why you'd bother loading plyr... assuming your data frame is df... (stole the wise use of min() from Shane's answer)

maxCol <- ncol(df)
for (i in seq(1, maxCol, by 3000)) {
     write.table(df[,i:min(i+2999, maxCol)], "i")
}

You may want to edit the write.table command above to add in your preferred formatting.

John
`plyr` is unnecessary in this example (could have used almost any apply function), but it's worth knowing in general (which is why I used it for demonstration). That said, `for` loops allow side-effects so I try to avoid them unless I want to encourage what could result in "bad" behavior.
Shane