Motivation: during each iteration, my code needs to collect tabular data (and use it only during that iteration), but the rows of data may vary. I thought I would speed it up by preinitializing the matrix that collects the data with zeros to what I know to be the maximum number of rows. I was surprised by what I found...
# set up (not the puzzling part)
x<-matrix(runif(20),nrow=4); y<-matrix(0,nrow=12,ncol=5); foo<-c();
# this is what surprises me... what the?
system.time(for(i in 1:100000){n<-sample(1:4,1);y[1:n,]<-x[1:n,];});
user system elapsed
1.510 0.000 1.514
system.time(for(i in 1:100000){n<-sample(1:4,1);foo<-x[1:n,];});
user system elapsed
1.090 0.000 1.085
These results are very repeatable. So, if I'm interpreting them correctly, dynamically allocating 'foo' each time to whatever the current output size is runs faster than writing to a subset of a preallocated 'y'? How is that possible?