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
On Thu, Sep 22, 2005 at 10:21:06AM -0400, Leite,Walter wrote:> > 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.paste("file", 1:1000, ".txt", sep="")> Thank you for your help, > > Walter L. Leite > > ______________________________________________ > 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-- - : Hao Chen, Ph.D. : Instructor : Department of Pharmacology : University of Tennessee Health Science Center : Memphis, TN 38163 USA : Office: 901 448 3201 : Mobil: 901 826 1845 Mining PubMed: http://www.chilibot.net -
as a toy example : for (i in 1:nbfiles) { fullname = paste("myfile",i, sep=""); write.table(intable,fullname,col.names=F,row.names=F,quote=F); } see ?paste hih
You can use paste() with something like formatC(number,digits=0,wid=3,flag="0") (where number is your loop index) to generate the filenames. Equipment maintenance, on 9/22/2005 10:21 AM Leite,Walter said the following:>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. ://www.R-project.org/posting-guide.html > >-- Michael Prager, Ph.D. Population Dynamics Team, NMFS SE Fisheries Science Center NOAA Center for Coastal Fisheries and Habitat Research Beaufort, North Carolina 28516 http://shrimp.ccfhrb.noaa.gov/~mprager/
> 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. LeiteIf 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) HTH, Andy> ______________________________________________ > 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 > > >
Walter -- P.S. The advantage of using formatC over pasting the digits (1:1000) directly is that when one uses leading zeroes, as in the formatC example shown, the resulting filenames will sort into proper order. ...MHP You can use paste() with something like formatC(number,digits=0,wid=3,flag="0") (where number is your loop index) to generate the filenames. on 9/22/2005 10:21 AM Leite,Walter said the following:>I have a question about how to save to the hard drive the one thousand >datasets I generated in a simulation. ://www.R-project.org/posting-guide.html
Others have shown how to create a vector of names, but here are a couple of other things you might want to look at. If you are only using these datasets within R, then you might consider not saving the data, just the random seeds, then when you need the data again you can just generate it. Also look at the g.data package, it provides methods for automatically saving and loading datasets. Another option for creating filenames is the tempfile command. hope this helps, Greg Snow, Ph.D. Statistical Data Center, LDS Hospital Intermountain Health Care greg.snow at ihc.com (801) 408-8111>>> "Leite,Walter" <Walter.Leite at coe.ufl.edu> 09/22/05 08:21AM >>>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 ______________________________________________ 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
On 22-Sep-05 Mike Prager wrote:> Walter -- > > P.S. The advantage of using formatC over pasting the digits (1:1000) > directly is that when one uses leading zeroes, as in the formatC > example > shown, the resulting filenames will sort into proper order. > > ...MHP > > > You can use paste() with something like > > formatC(number,digits=0,wid=3,flag="0") > > (where number is your loop index) to generate the filenames. > > > on 9/22/2005 10:21 AM Leite,Walter said the following: > >>I have a question about how to save to the hard drive the one thousand >>datasets I generated in a simulation. >>://www.R-project.org/posting-guide.htmlFor this precise question, the replies for filename creation, though useful, have been slightly off-target. Walter may presumably want the ilenames to be in sortable order corresponding to the numerical order of creation, i.e. like file0001 file0002 ... file1000 The precise formatC specification required for this would be formatC(n,digits=0,wid=4,format="d",flag="0") so that formatC(1,digits=0,wid=4,format="d",flag="0") # [1] "0001" -> "file0001" formatC(999,digits=0,wid=4,format="d",flag="0") # [1] "0999" -> "file0999" formatC(1000,digits=0,wid=4,format="d",flag="0") # [1] "1000" -> "file1000" The suggestions with "wid=3" would give formatC(999,digits=0,wid=3,format="d",flag="0") # [1] "999" -> "file999" formatC(1000,digits=0,wid=3,format="d",flag="0") # [1] "1000" -> "file1000" which are now in the wrong order (since "file1000" sorts alphabetically prior to "file999". Also, if "format=d" is not specified we get things like formatC(100,digits=0,wid=3,flag="0") # [1] "1e+02" -> "file1e+02" which, while a valid filename, is on its head for sorting (since now the exponent sorts fastest!). Best wishes to all, Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk> Fax-to-email: +44 (0)870 094 0861 Date: 22-Sep-05 Time: 17:51:36 ------------------------------ XFMail ------------------------------