If I have 100 objekts in a folder and I prefer not to load them manually I wonder how I do this in R if using a for-loop. I was thinking initially to do something like this: infiles <- dir(pattern=".RData") for(i in length(infiles)) { load(infiles[i]) paste("kalle", i, sep="") <- saveLoadReference } But the line """paste("kalle", i, sep="")""" does not do it for me. I get the error message "Error: Target of assignment expands to non-language object" The thing that I do not master is how to create a name in a for-loop that I can assign something to. And I want to be able to change that name as the loop goes on. I want to create in this case kalle1 kalle2 kalle3 ... kalle100 and they should all represent the objects that I opened with load(infiles[i]) Best regards / Johan ******************************************************************************************* Johan Lindberg Royal Institute of Technology AlbaNova University Center Stockholm Center for Physics, Astronomy and Biotechnology Department of Molecular Biotechnology 106 91 Stockholm, Sweden Phone (office): +46 8 553 783 45 Fax: + 46 8 553 784 81 Visiting adress: Roslagstullsbacken 21, Floor 3 Delivery adress: Roslagsv?gen 30B
Johan Lindberg wrote:> If I have 100 objekts in a folder and I prefer not to load them manually > I wonder how I do this in R if using a for-loop. > > I was thinking initially to do something like this: > > infiles <- dir(pattern=".RData")For sure you mean infiles <- dir(pattern = "\\.RData")> for(i in length(infiles))This won't work. I'd try for(i in 1:length(infiles)) or much better: for(i in seq(along = infiles))> { > load(infiles[i]) > paste("kalle", i, sep="") <- saveLoadReferenceWhatever saveLoadReference is ... try assign(paste("kalle", i, sep=""), saveLoadReference) Please note that it might be a good idea to use a list "kalle" with elements corresponding to the different "saveLoadReference" objects. So that you don't mess up you workspace with many objects .... Uwe Ligges> } > > But the line """paste("kalle", i, sep="")""" does not do it for me. I > get the error message "Error: Target of assignment expands to > non-language object" > > The thing that I do not master is how to create a name in a for-loop > that I can assign something to. And I want to be able to change that > name as the loop goes on. I want to create in this case > kalle1 > kalle2 > kalle3 > ... > kalle100 > > and they should all represent the objects that I opened with > load(infiles[i]) > > > Best regards > > / Johan > > > > ******************************************************************************************* > > Johan Lindberg > Royal Institute of Technology > AlbaNova University Center > Stockholm Center for Physics, Astronomy and Biotechnology > Department of Molecular Biotechnology > 106 91 Stockholm, Sweden > > Phone (office): +46 8 553 783 45 > Fax: + 46 8 553 784 81 > Visiting adress: Roslagstullsbacken 21, Floor 3 > Delivery adress: Roslagsv?gen 30B > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html
You have two problems. 1) You want to use assign(paste("kalle", i, sep=""), saveLoadReference) to create the name. That one is discussed frequently enough to be an FAQ, and it is Q7.23. 2) You need to keep the return value of load to assign to the object. So I think you want for(i in length(infiles)) assign(paste("kalle", i, sep=""), load(infiles[i])) However, that assigns to kalle{n} the names(s) of the objects you loaded. Is that what you actually wanted? Or did you want the actual objects, not their `representation'. If you want the actual objects, I suggest you use .readRDS instead. On Thu, 15 Jan 2004, Johan Lindberg wrote:> If I have 100 objekts in a folder and I prefer not to load them manually I > wonder how I do this in R if using a for-loop. > > I was thinking initially to do something like this: > > infiles <- dir(pattern=".RData") > for(i in length(infiles)) > { > load(infiles[i]) > paste("kalle", i, sep="") <- saveLoadReference > } > > But the line """paste("kalle", i, sep="")""" does not do it for me. I get > the error message "Error: Target of assignment expands to non-language object" > > The thing that I do not master is how to create a name in a for-loop that I > can assign something to. And I want to be able to change that name as the > loop goes on. I want to create in this case > kalle1 > kalle2 > kalle3 > ... > kalle100 > > and they should all represent the objects that I opened with load(infiles[i])-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595