[R] save and load in R

Ivan Calandra ivan.calandra at uni-hamburg.de
Mon Jun 20 10:12:18 CEST 2011


Hi,

Check also saveObject() and loadObject() from R.utils:

library(R.utils)
outfilenames<- paste ("./file", 1:numFiles, '.Rbin', sep="")

for ( i in 1:10) {
   dataFile = read.table (filenames[i], header=TRUE, sep='\t');
   saveObject (dataFile, file = outfilenames[i])
}

And then:
file1 = loadObject (file = './file1.Rdata')

HTH,
Ivan


Le 6/19/2011 17:07, Mary Kindall a écrit :
> Thanks  Jeff and Duncan
> Assign worked for me. I did not check the other methods suggested by you.  I
> am repreducing my code below:
>
> ##################
> filenames = list.files(path = ".", pattern = '.txt$', all.files = FALSE,
> full.names = TRUE, ignore.case = FALSE)  #all input files
> numFiles = length(filenames)
>
> outfilenames<- paste("./file", 1:numFiles, '.Rdata', sep="") # output files
>
>
> for ( i in 1:numFiles)
> {
> dataFile = read.table(filenames[i], header=TRUE, sep='\t');
> save(dataFile, file = outfilenames[i])   #Saving into the output files
> }
>
>
> newnames<- paste("file", 1:numFiles, sep="")  #output variables to load
> into
>
> for ( i in 1:numFiles)
> {
> load(file = outfilenames[i]);
> assign(newnames[i], dataFile)   #assign into corresponding output variables;
>
> }
> #####################
>
>
> Regards
> -
> M
>
>
>
> On Sun, Jun 19, 2011 at 10:38 AM, Duncan Murdoch
> <murdoch.duncan at gmail.com>wrote:
>
>> On 11-06-19 10:26 AM, Mary Kindall wrote:
>>
>>> I have a list of txt files that I want to convert into .rdata R data
>>> object.
>>>
>>> filenames
>>> 1. "./file1.txt"
>>> 2. "./file2.txt"
>>> 3. "./file3.txt"
>>> 4. "./file4.txt"
>>> 5. "./file5.txt"
>>> 6. "./file6.txt"
>>> 7. "./file7.txt"
>>> 8. "./file8.txt"
>>> 9. "./file9.txt"
>>> 10. "./file10.txt"
>>>
>>> I saved these files as
>>>
>>> for ( i in 1:10)
>>> {
>>> dataFile = read.table(filenames[i], header=TRUE, sep='\t');
>>> save (dataFile, file = outfilenames[i])
>>> }
>>>
>>> The inpt files are saves as:
>>> outfilenames
>>> 1. "./file1.Rdata"
>>> 2. "./file2.Rdata"
>>> 3. "./file3.Rdata"
>>> 4. "./file4.Rdata"
>>> 5. "./file5.Rdata"
>>> 6. "./file6.Rdata"
>>> 7. "./file7.Rdata"
>>> 8. "./file8.Rdata"
>>> 9. "./file9.Rdata"
>>> 10. "./file10.Rdata"
>>>
>>>
>>> Now I want to load these out files in such a way that the data is loaded
>>> into a variable that is same as the file name without extension.
>>>
>>> file1 = load (file = './file1.Rdata')
>>> file2 = load (file = './file2.Rdata')
>>> file3 = load (file = './file3.Rdata')
>>> file4 = load (file = './file4.Rdata')
>>>
>>> How can I do that.
>>>
>> When you load() a file, the variables in it are restored with the same
>> names that were saved.  So you would need something like
>>
>> newnames<- paste("file", 1:10, sep="") # file1, file2, etc.
>>
>>
>> for (i in 1:10) {
>>   load(file=outfilenames[i]) # assuming that's still around...
>>   assign(newnames[i], dataFile)
>> }
>>
>> It would be a little simpler to use saveRDS() and readRDS() to save and
>> load your files.  They don't save the object names.
>>
>> A more R-like version of this would be to create a list of datasets, e.g.
>>
>> files<- list()
>>
>> for (i in 1:10) {
>>   load(file=outfilesnames[i])
>>   files[[i]]<- dataFile
>> }
>>
>> Then you don't end up creating 10 objects, but you can still access them
>> separately.
>>
>> Duncan Murdoch
>>
>>
>

-- 
Ivan CALANDRA
PhD Student
University of Hamburg
Biozentrum Grindel und Zoologisches Museum
Dept. Mammalogy
Martin-Luther-King-Platz 3
D-20146 Hamburg, GERMANY
+49(0)40 42838 6231
ivan.calandra at uni-hamburg.de

**********
http://www.for771.uni-bonn.de
http://webapp5.rrz.uni-hamburg.de/mammals/eng/1525_8_1.php



More information about the R-help mailing list