Re: [R] Automatic creation of file names

From: Liaw, Andy <>
Date: Fri 23 Sep 2005 - 00:38:07 EST

> From: Leite,Walter
> Dear R-Help members,
> I have a question about how to save to the hard drive the one thousand
> datasets I generated in a simulation. The datasets are created in a
> "for" loop that repeatedly creates normally distributed datasets, such
> as the example below:
> Library(MASS)
> for (number in 1:1000) {
> dataset = mvrnorm(n = 400, mu = c(0,0,0),
> Sigma = matrix(c(1,0.3,0.3,0.3,1,0.3,0.3,0.3,1),3,3))
> }
> I don't know how to write the datasets with different names
> to the hard
> drive. I have solved the problem before by creating a list of names
> (file1.txt, file2.txt,file3.txt, etc...) in Microsoft Excel,
> reading it
> to R with read.table, and using write.table to write each
> file with the
> name I created in Excel.
> I want to know if it is possible to create a list of file names
> automatically within R to save the datasets created in my simulation.
> Thank you for your help,
> Walter L. Leite

If the total size of all the data isn't too large, that you can load all of them into memory without problem, I'd suggest that you put them into a list and just save the list.

If you really only want to use one of the datasets at a time, just insert something like the following into the loop:

save(dataset, file=paste("data", number, ".rda", sep=""), compress=TRUE)


> ______________________________________________
> mailing list
> PLEASE do read the posting guide!
> mailing list PLEASE do read the posting guide! Received on Fri Sep 23 00:59:01 2005

This archive was generated by hypermail 2.1.8 : Sun 23 Oct 2005 - 17:39:10 EST