Skip to content
Prev 24671 / 398502 Next

create an object list in a loop

Peter Dalgaard BSA <p.dalgaard at biostat.ku.dk> writes:
Sorry, forgot to say that the construct

 aval<-list()
 for(i in 1:n) aval[[i]]<-.....

is distictly bad since it requires a reallocation of the list every
time you add to it. If you have to do it that way, use 

aval <- vector("list", n)

to create aval with the correct length from the outset. (Of course
this is just another good reason to prefer lapply() ... .)