[R] filling a list faster

Peter Dalgaard P.Dalgaard at biostat.ku.dk
Fri Jul 13 14:01:55 CEST 2007


Balazs Torma wrote:
> Thank you all for your answers!
>
> The problem is that I don't know the length of the list in advance!  
> And hoped for a convinience structure which reallocates once the  
> preallocated list (or matrix) becomes full.
>   
That's not massively hard to do yourself, is it?
As in

if (i > N) {l <- c(l, vector("list",N); N <- N*2}

i.e.
> N<-1; l <- vector("list", N)
> system.time(for(i in (1:1e5)) { if (i > N) {l <- c(l,
vector("list",N)); N <- N*2} ; l[[i]] <- c(i,i+1,i)})
   user  system elapsed
  1.508   0.012   1.520
> l[i+1:N]<-NULL

-- 
   O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)                  FAX: (+45) 35327907



More information about the R-help mailing list