[R] Save output

Mike Lawrence Mike.Lawrence at Dal.Ca
Mon Oct 3 14:00:57 CEST 2005


I think this is what you're looking for:

file_name_root = "myfile"

for(i in 1:10)

	result<- of_some_function

	out_file<-paste(file_name_root, i, ".txt", sep = "")

	write.table(result, file = out_file)

}

the above code produces 10 files ("myfile1.txt" to "myfile10.txt"), each
containing whatever was calculated on that loop. If you have loops 
within loops
and you want to write during the innermost loop, remember to take this into
account like so:

file_name_root = "myfile"

for (i in 1:10)

	for(q in 1:10)

		result<- of_some_function

		out_file<-paste(file_name_root, i, q, ".txt", sep = "")

		write.table(result, file = out_file)

	}

}

That way, when "i" iterates and when "q" resets to 1, you don't write 
over your
previous output.


Quoting Frank Schmid <frank.schmid at vwi.unibe.ch>:

> Dear R-Mastermind
>
>
> Within a while or a for-loop, is there a way that I can save to disk the
> results of the previous calculations at the end of each loop with filenames
> called "file01.Rdata", "file02.Rdata" etc?
>
> So far, I have tried to write the outcome of each loop in a 3 dimensional
> array and saved it just once after the loop. Or is there another way so that
> I can keep the resulting matrix of every step in the loop?
>
> Thank you very much for your help
>
>
> Frank Schmid
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>



-- 

Mike Lawrence, BA(Hons)
Research Assistant to Dr. Gail Eskes
Dalhousie University & QEII Health Sciences Centre (Psychiatry)

Mike.Lawrence at Dal.Ca

"The road to Wisdom? Well, it's plain and simple to express:
Err and err and err again, but less and less and less."
- Piet Hein




More information about the R-help mailing list