I have around 200 data frames I want to rbind in a vectorized way. The object names are: m302 m303 ... m500 So I tried: mlist <- paste("m",302:500,sep="") dat <- do.call("rbind", get(list(mlist))) and I get "Error in get(x, envir, mode, inherits) : invalid first argument" I know "rbind" is valid because dat <- rbind(m302, m303, m304, m305) works, I am just too lazy to type it out to m500. I also tried it without the get() portion, but then dat ends up being a column with just the names of the objects, not the objects themselves. Thanks in advance for showing me the errors in my attempts. Roger
Here's one way (should work for data frames, too):> mlist <- paste("m", 1:5, sep="") > for (i in 1:5) assign(mlist[i], matrix(runif(4), 2, 2)) > do.call("rbind", mget(mlist, .GlobalEnv))[,1] [,2] [1,] 0.33846095 0.09494054 [2,] 0.65229451 0.26009002 [3,] 0.53969914 0.66481544 [4,] 0.59316498 0.36125843 [5,] 0.24005411 0.01010594 [6,] 0.06878842 0.93929806 [7,] 0.02618064 0.06243190 [8,] 0.08144251 0.04331587 [9,] 0.95216684 0.61066435 [10,] 0.03712678 0.95809995 get() takes a character vector as the first argument, and you passed it a list with one component (a character vector). Even if you pass mlist to get(), it will only get the first one. Andy> From: roger bos > > I have around 200 data frames I want to rbind in a vectorized way. > The object names are: > > m302 > m303 > ... > m500 > > So I tried: > > mlist <- paste("m",302:500,sep="") > dat <- do.call("rbind", get(list(mlist))) > > and I get "Error in get(x, envir, mode, inherits) : invalid > first argument" > > I know "rbind" is valid because > > dat <- rbind(m302, m303, m304, m305) > > works, I am just too lazy to type it out to m500. > > I also tried it without the get() portion, but then dat ends up being > a column with just the names of the objects, not the objects > themselves. > > Thanks in advance for showing me the errors in my attempts. > > Roger > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > >
Roger: Please re-read ?get, where it says that the x argument must be a single character string name, not a list.You must first create the list using get and then call do.call on that list: do.call("rbind",lapply(mlist,get)) -- Bert Gunter Genentech Non-Clinical Statistics South San Francisco, CA "The business of the statistician is to catalyze the scientific learning process." - George E. P. Box> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of roger bos > Sent: Friday, January 21, 2005 9:07 AM > To: r-help at stat.math.ethz.ch > Subject: [R] how to use do.call("rbind", get(list(mlist))) > > I have around 200 data frames I want to rbind in a vectorized way. > The object names are: > > m302 > m303 > ... > m500 > > So I tried: > > mlist <- paste("m",302:500,sep="") > dat <- do.call("rbind", get(list(mlist))) > > and I get "Error in get(x, envir, mode, inherits) : invalid > first argument" > > I know "rbind" is valid because > > dat <- rbind(m302, m303, m304, m305) > > works, I am just too lazy to type it out to m500. > > I also tried it without the get() portion, but then dat ends up being > a column with just the names of the objects, not the objects > themselves. > > Thanks in advance for showing me the errors in my attempts. > > Roger > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html >
roger bos wrote:> I have around 200 data frames I want to rbind in a vectorized way. > The object names are: > > m302 > m303 > ... > m500 > > So I tried: > > mlist <- paste("m",302:500,sep="") > dat <- do.call("rbind", get(list(mlist)))do.call("rbind", lapply(mlist, get)) Uwe Ligges> and I get "Error in get(x, envir, mode, inherits) : invalid first argument" > > I know "rbind" is valid because > > dat <- rbind(m302, m303, m304, m305) > > works, I am just too lazy to type it out to m500. > > I also tried it without the get() portion, but then dat ends up being > a column with just the names of the objects, not the objects > themselves. > > Thanks in advance for showing me the errors in my attempts. > > Roger > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
get() doesn't take a list. You need to wrap it in an lapply so that it grabs the dataframes individually and then wraps it into a list for do.call(). The easy way, however, that doesn't involve do.call or get is to simply construct the proper text rbind query and then run it with eval. eval(parse(text = paste("rbind(", paste("m", 302:500, sep = "", collapse = ", "), ")"))) To use the do.call() / get() construct, I think this is what you were thinking of: mlist <- paste("m",302:500,sep="") do.call("rbind", lapply(mlist, get)) Best, Robert -----Original Message----- From: roger bos [mailto:roger.bos at gmail.com] Sent: Friday, January 21, 2005 12:07 PM To: r-help at stat.math.ethz.ch Subject: [R] how to use do.call("rbind", get(list(mlist))) I have around 200 data frames I want to rbind in a vectorized way. The object names are: m302 m303 ... m500 So I tried: mlist <- paste("m",302:500,sep="") dat <- do.call("rbind", get(list(mlist))) and I get "Error in get(x, envir, mode, inherits) : invalid first argument" I know "rbind" is valid because dat <- rbind(m302, m303, m304, m305) works, I am just too lazy to type it out to m500. I also tried it without the get() portion, but then dat ends up being a column with just the names of the objects, not the objects themselves. Thanks in advance for showing me the errors in my attempts. Roger ______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Roger -- First, if you have the data frames in a list already, you can just use those; no need to bother with the names. If the data frames are all separate, but you have the names, you can first create a list of the data frames themselves: do.call("rbind", lapply(mlist, get)) # assumes they're in the workspace An example:> temp1 <- matrix(runif(10), 5, 2) > temp2 <- matrix(-runif(10), 5, 2) (for easy identifiability) > temp3 <- do.call("rbind", lapply(c("temp1", "temp2"), get)) > temp3 <- do.call("rbind", list(temp1, temp2)) (same as above)Hope this helps, Matt Wiener -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of roger bos Sent: Friday, January 21, 2005 12:07 PM To: r-help at stat.math.ethz.ch Subject: [R] how to use do.call("rbind", get(list(mlist))) I have around 200 data frames I want to rbind in a vectorized way. The object names are: m302 m303 ... m500 So I tried: mlist <- paste("m",302:500,sep="") dat <- do.call("rbind", get(list(mlist))) and I get "Error in get(x, envir, mode, inherits) : invalid first argument" I know "rbind" is valid because dat <- rbind(m302, m303, m304, m305) works, I am just too lazy to type it out to m500. I also tried it without the get() portion, but then dat ends up being a column with just the names of the objects, not the objects themselves. Thanks in advance for showing me the errors in my attempts. Roger ______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
On Fri, 21 Jan 2005, roger bos wrote:> I have around 200 data frames I want to rbind in a vectorized way. > The object names are: > > m302 > m303 > ... > m500 > > So I tried: > > mlist <- paste("m",302:500,sep="") > dat <- do.call("rbind", get(list(mlist))) > > and I get "Error in get(x, envir, mode, inherits) : invalid first argument"Yep. get() takes a single variable name as its argument. If you tried get(list("m302","m303")) you would get the same error.> I know "rbind" is valid because > > dat <- rbind(m302, m303, m304, m305) > > works, I am just too lazy to type it out to m500. > > I also tried it without the get() portion, but then dat ends up being > a column with just the names of the objects, not the objects > themselves.Indeed. This translates to rbind("m302","m303".... You could use mget() instead of get(). -thomas