[R] Writing out in loops

Thomas chesney.alt at gmail.com
Tue Dec 6 21:02:49 CET 2011


Thanks for the reply. I'm writing data out as it is generated into a number of different files and not saving it as I go (although I could - not sure which would be quicker). In fact I tried this today and  the approach with the loop seems to run reasonably fast.

Thomas

On 6 Dec 2011, at 15:07, Sarah Goslee wrote:

> Is there some reason you can't create the entire file in R and then write it
> out in a single step?
> 
> It looks like you're writing out
> mat[1, 1:10]
> so you could just use
> write.table(mat[1, 1:10], filen)
> 
> Can you expand on what you're trying to do?
> 
> Sarah
> 
> On Tue, Dec 6, 2011 at 4:18 AM, Thomas <chesney.alt at gmail.com> wrote:
>> I would like to write some data to different files. I can create the
>> filename Day1.txt like this:
>> 
>> filen <- paste("Day", l, ".txt", sep="")
>> 
>> and then I'm using a For loop to write out one row of a matrix, something
>> like this:
>> 
>> For (j in 1:10)
>> {
>> cat(mat[1,j], ",", file=filen, append=TRUE)
>> }
>> cat("\n", file=filen, append=TRUE)
>> 
>> but is there a better way to do this?
>> 
>> Thank you,
>> 
>> Thomas
>> 
> 
> 
> -- 
> Sarah Goslee
> http://www.functionaldiversity.org



More information about the R-help mailing list