Skip to content
Prev 28162 / 398498 Next

Change array size

If you just want to extend the vector, and fill new elements with NA, then 
a simple way is:

 > x <- 1:5
 > length(x) <- 2 ^ ceiling(log(length(x))/log(2))
 > x
[1]  1  2  3  4  5 NA NA NA
 >

However, one needs to be careful with kind of arithmetic -- floating point 
inaccuracies can cause unexpected results.  E.g., one would expect the 
following points to form a straight line, but they don't -- on my computer 
there are 4 anomalies between 29 and 50:

 > plot(ceiling(log(2**(1:50))/log(2)))

In the question asked, this is unlikely to cause problems as it is unlikely 
that a vector will have 2^29 elements.
At Friday 09:51 AM 2/14/2003 +0000, Poizot Emmanuel wrote: