[R] how to use do.call("rbind", get(list(mlist)))

Liaw, Andy andy_liaw at merck.com
Fri Jan 21 19:10:36 CET 2005


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
> 
>




More information about the R-help mailing list