Jeremy Ng
2013-Jun-01 10:18 UTC
[R] Loading an .RData file and assigning the loaded objects within a loop
Hi all, I tried to look this up online, but am still feeling a little stuck. I have a bunch of Rdata files in side my directory, and I would like to load all of them in a loop. Each time the next is read in, it would over-write the previously read in object because they will be assigned the same workspace object name. Is there anyway for each individually read in Rdata object to be assigned a new name? Thanks! JEremy [[alternative HTML version deleted]]
Jim Holtman
2013-Jun-01 10:52 UTC
[R] Loading an .RData file and assigning the loaded objects within a loop
use 'lapply' to read in the .RData file. Therefore each file is stored as an element of a list thatb you can access and change as necessary. Sent from my iPad On Jun 1, 2013, at 6:18, Jeremy Ng <jeremy.ng.wk1990 at gmail.com> wrote:> Hi all, > > I tried to look this up online, but am still feeling a little stuck. > > I have a bunch of Rdata files in side my directory, and I would like to > load all of them in a loop. Each time the next is read in, it would > over-write the previously read in object because they will be assigned the > same workspace object name. > > Is there anyway for each individually read in Rdata object to be assigned a > new name? > > Thanks! > JEremy > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.
Barry Rowlingson
2013-Jun-01 15:02 UTC
[R] Loading an .RData file and assigning the loaded objects within a loop
Within lapply load the file and return the value of the thing or things in the file you want. Something like this: > files [1] "x1.RData" "x2.RData" "x3.RData" "x4.RData" > lapply(files,function(z){load(z);x}) [[1]] [1] 1 [[2]] [1] 22 [[3]] [1] 333 [[4]] [1] 4444 Each of my files contains a single number stored in an object called 'x'. Within the loop 'x' has the value just loaded from the file. Another method is to use the environment argument of load to save into an environment other than the global one... On Sat, Jun 1, 2013 at 11:52 AM, Jim Holtman <jholtman at gmail.com> wrote:> use 'lapply' to read in the .RData file. Therefore each file is stored as an element of a list thatb > you can access and change as necessary. > > Sent from my iPad > > On Jun 1, 2013, at 6:18, Jeremy Ng <jeremy.ng.wk1990 at gmail.com> wrote: > >> Hi all, >> >> I tried to look this up online, but am still feeling a little stuck. >> >> I have a bunch of Rdata files in side my directory, and I would like to >> load all of them in a loop. Each time the next is read in, it would >> over-write the previously read in object because they will be assigned the >> same workspace object name. >> >> Is there anyway for each individually read in Rdata object to be assigned a >> new name? >> >> Thanks! >> JEremy >> >> [[alternative HTML version deleted]] >> >> ______________________________________________ >> 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. > > ______________________________________________ > 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.
Gabor Grothendieck
2013-Jun-01 15:42 UTC
[R] Loading an .RData file and assigning the loaded objects within a loop
On Sat, Jun 1, 2013 at 6:18 AM, Jeremy Ng <jeremy.ng.wk1990 at gmail.com> wrote:> Hi all, > > I tried to look this up online, but am still feeling a little stuck. > > I have a bunch of Rdata files in side my directory, and I would like to > load all of them in a loop. Each time the next is read in, it would > over-write the previously read in object because they will be assigned the > same workspace object name. > > Is there anyway for each individually read in Rdata object to be assigned a > new name? >If Files is a character vector containing the filenames then this creates a list witih one componet per RData file. The list names will be the file names. Each component is itself a list containing the objects in that file: L <- sapply(Files, function(x) mget(load(x)), simplify = FALSE) (If it is known that each RData file contains only a single object then mget could be replaced with get in which case each component of L would be the object in the respective file. In that case you will lose the object names but as you indicated they are all the same that likely won' t matter.) -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com