[R] creating list with 200 identical objects

Chuck Cleland ccleland at optonline.net
Mon Jun 1 12:27:46 CEST 2009


On 6/1/2009 6:08 AM, Rainer M Krug wrote:
> Hi
> 
> I am doing an simulation, and I a large proportion of the simulation
> time is taken up by memory allocations.
> 
> I am creating an object, and storing it in a list of those objects.
> 
> essentially:
> 
> x <- list()
> for (t in 1:500) {
>  x[1] <- new("track")
> }
> 
> I would like to initialize in one go, to avoid the continuous
> reallocation of memory when a new "track" is added, and fill it wit
> the object created by new("track").
> 
> How can I do this?

  Does this help?

x <- vector("list", 500)
for(i in 1:500){x[[i]] <- runif(30)}

> thanks
> 
> Rainer 

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