[R] Problem accessing .Rdata objects in a loop

R. Michael Weylandt michael.weylandt at gmail.com
Tue Apr 17 18:21:19 CEST 2012


I'm not sure that's francy's problem. This seems to work for me:

# Some fake data
nms <- letters[1:5]
lapply(nms, function(x) assign(x, rnorm(10), .GlobalEnv))

# Make some .Rdata files
lapply(nms, function(x) save(list =x, file = paste0(x, ".Rdata")))

# Check they are there
list.files()

# Bring them in and do operations
for(nm in nms){
    load(paste0(nm,".Rdata"))
   print(max(get(nm))) # Works
}

Francy's problem seems to be trying to use `n` (the symbol, not the
character vector) to access the loaded objects instead of get. Use of
the backticks doesn't do what I think you (Francy) think it does here.

Of course, I think this is all a little easier if one uses saveRDS /
readRDS which can assign directly to an object upon load (xxx <-
loadRDS(filename)) -- Duncan Murdoch pointed these out to me just a
few weeks ago.

Michael

On Tue, Apr 17, 2012 at 12:07 PM, Bert Gunter <gunter.berton at gene.com> wrote:
> Try reading ?load again.
> It takes a single filename only.
>
> Also 'n' is a character, not a symbol for a variable.  I think you
> need to do some reading on basic programming. Try An Introduction to R
> (part of the distro) to get you going.
>
> -- Bert
>
> On Tue, Apr 17, 2012 at 8:38 AM, francy <francy.casalino at gmail.com> wrote:
>> Hi,
>>
>> I am trying to access many .Rdata objects and do some operations with them
>> using a loop. I can load the files but can't access them. The files' names
>> are stored in a character vector called "names".  After loading the objects,
>> I can view each one using ls() and see that two objects are present for
>> each. I am trying to access the one with the name which is the same as the
>> name of the .Rdata. I can access individual .Rdata object using backticks
>> around its name like this:
>>
>> names<- c("df1", "df2", "df3")
>> load(paste(path, "/", df1, ".RData", sep="")
>> df<- `df1`
>>
>> But when I try using the variable name in a loop it does not work:
>>
>> for (n in names) {
>> print(names)
>> load(paste(path,  "/", names, ".RData", sep=""))
>> df<- `n`
>> ###do some other operations with each object
>> }
>>
>> Could you please help me understand what I can do to view these objects?
>>
>> Thank you.
>>
>> --
>> View this message in context: http://r.789695.n4.nabble.com/Problem-accessing-Rdata-objects-in-a-loop-tp4565154p4565154.html
>> Sent from the R help mailing list archive at Nabble.com.
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>
>
>
> --
>
> Bert Gunter
> Genentech Nonclinical Biostatistics
>
> Internal Contact Info:
> Phone: 467-7374
> Website:
> http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list