[R] Writing out in loops

Sarah Goslee sarah.goslee at gmail.com
Tue Dec 6 16:07:23 CET 2011

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?


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

More information about the R-help mailing list