[R] Change array size

Tony Plate tplate at blackmesacapital.com
Fri Feb 14 18:32:04 CET 2003


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:
>Hi,
>I would like to know if there is a way to change a vector of arbitrary size
>to make it fits the nearest upper size multiple of a power of 2.
>
>--
>Cordialy
>----------------------------------------
>Emmanuel POIZOT
>Cnam/Intechmer
>Digue de Collignon
>50110 Tourlaville
>Tél : (33)(0)2 33 88 73 42
>Fax : (33)(0)2 33 88 73 39
>-----------------------------------------
>
>______________________________________________
>R-help at stat.math.ethz.ch mailing list
>http://www.stat.math.ethz.ch/mailman/listinfo/r-help




More information about the R-help mailing list