[R] creating a list of matrices or data frames

Chuck Cleland ccleland at optonline.net
Tue Jan 20 18:36:59 CET 2009


On 1/20/2009 11:34 AM, Simon Pickett wrote:
> Hi all,
> 
> How would you create a list of data.frames within a loop, then bind all
> the elements of the list using rbind?
> 
> take this example of matrices with differing numbers of rows
> 
> for(i in 1:3){
> assign(paste("s",i, sep=""),matrix(data = NA, nrow = i, ncol = 3, byrow
> = FALSE, dimnames = NULL))
> }
> s1
> s2
> s3
> 
> I want to bind all the matrices at the end with do.call(rbind...) 
> rather than listing all the elements manually with rbind(s1,s2,s3...)
> and so on.
> 
> thanks in advance.

df.list <- vector("list", 3) # create list

for(i in 1:3){df.list[[i]] <- matrix(data = NA,
                                     nrow = i,
                                     ncol = 3,
                                     byrow = FALSE,
                                     dimnames = NULL)}

do.call(rbind, df.list) # rbind list elements

> Simon.
> 
> ______________________________________________
> 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. 

-- 
Chuck Cleland, Ph.D.
NDRI, Inc. (www.ndri.org)
71 West 23rd Street, 8th floor
New York, NY 10010
tel: (212) 845-4495 (Tu, Th)
tel: (732) 512-0171 (M, W, F)
fax: (917) 438-0894




More information about the R-help mailing list