Thank you both for your answers. Sorry if I haven't explained myself well, Michael, that is what I was looking for. Using get(n) works, thank you. I wish I could have used saveRDS and readRDS, but it was not my saved object⦠In any case thanks again.
2012/4/17 Michael Weylandt [via R] <ml-node+s789695n4565280...@n4.nabble.com>: > 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 <[hidden email]> 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 <[hidden email]> 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. >>> >>> ______________________________________________ >>> [hidden email] 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 >> >> ______________________________________________ >> [hidden email] 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. > > ______________________________________________ > [hidden email] 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. > > > ________________________________ > If you reply to this email, your message will be added to the discussion > below: > http://r.789695.n4.nabble.com/Problem-accessing-Rdata-objects-in-a-loop-tp4565154p4565280.html > To unsubscribe from Problem accessing .Rdata objects in a loop, click here. > NAML -- View this message in context: http://r.789695.n4.nabble.com/Problem-accessing-Rdata-objects-in-a-loop-tp4565154p4567321.html Sent from the R help mailing list archive at Nabble.com. [[alternative HTML version deleted]]
______________________________________________ R-help@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.