You can also use list syntax, env$name or env[["name"]], with environments to avoid the ugliness of get() and assign() and to ease your way over to at least using a dedicated environment (if not a list) for your related objects instead of filling the global environment to various collections of objects related only by name.
Here is an untested example: readFilesAndAssign <- function(envir, fileNames) { objNames <- sub("\\.[[:alpha:]]+", "", fileNames) # remove .csv, etc. for(i in seq_along(fileNames)) { envir[[ objNames[i] ]] <- read.csv(fileNames[i]) } envir } If you want all those objects in the global environment call this as readFilesAndAssign(.GlobalEnv, c("one.csv", "two.csv", "three.csv")) but if you want to keep things more organized (and not accidently overwrite data in the global environment) do objEnv <- new.env() readFilesAndAssign(objEnv, c("one.csv", "two.csv", "three.csv")) The same function works if the envir variable is a list, but you have to save its return value to keep its results objList <- readFilesAndAssign(list(), c("one.csv", "two.csv", "three.csv")) In all cases, if I have written this correctly, the function will read the files "one.csv", "two.csv", and "three.csv" and put the results into objects called "one", "two", and "three" into either the object or list that you supply. You can later access them with syntax like objList[["one"]] # objEnv or .GlobalEnv, depending on how you call it or with(objEnv, lm(data=one, y~x))) # or objList or .GlobalEnv, depending ... Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com > -----Original Message----- > From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On > Behalf Of Greg Snow > Sent: Friday, September 09, 2011 2:31 PM > To: MatAra; r-help@r-project.org > Subject: Re: [R] Looping through multiple datasets already in memory > > The best approach is to put all those datasets into a list, then you can loop > through the elements of > the list, or use lapply or sapply on the list. > > If you insist on keeping them outside of a list, or need a loop to put them > into the list, then look > at the 'get' function. > > -----Original Message----- > From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On > Behalf Of MatAra > Sent: Friday, September 09, 2011 12:26 PM > To: r-help@r-project.org > Subject: [R] Looping through multiple datasets already in memory > > Gents, > > I have the following loop: > > for (i in (seq(along=Draw.1[,1]))) {print(i)} # from 1 - 5 (counter) > for (i in (seq(along=Draw.1[,1]))) {print(Draw[i,4])} # from the 1 - column > (passes parameter per column) > > Now: I have multiple Draws [Draw.1 - Draw.100] in memory with different > "orders" > Question: How do I loop through each Draw.[j]? OR in other way, how do I > pick up all the datasets from memory dynamically? > > Something like: > for (i in (seq(along=Draw.[J][,1]))) {print(Draw[i,4])} > > paste() would not allow me to pick the [row,col] combination I need. It just > passes the list of all the elements. > paste(as.matrix(Draw), 1, sep=".")[10,5] DOES NOT WORK > > Thanks in advanced for the ideas! > > An example of how the draw (control files) looks like: > > Draw.1 > ID Name Tab Folder L/S > [1,] " 38" "Stoxx50_24_08_11" "38" "Stoxx" "-1" > [2,] " 47" "Stoxx50_24_08_11" "47" "Stoxx" " 1" > [3,] " 153" "DAX_31_08_11" "29" "DAX" " 1" > [4,] " 256" "FT100_31_08_11" "12" "UK" " 1" > [5,] " 303" "FT100_31_08_11" "59" "UK" " 1" > > > -- > View this message in context: > http://r.789695.n4.nabble.com/Looping-through-multiple-datasets-already- > in-memory-tp3802453p3802453.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. > > ______________________________________________ > 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. ______________________________________________ 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.