[R] Best way to preallocate numeric NA array?

Douglas Bates bates at stat.wisc.edu
Thu Nov 26 17:22:45 CET 2009


On Thu, Nov 26, 2009 at 10:03 AM, Rob Steele
<freenx.10.robsteele at xoxy.net> wrote:
> These are the ways that occur to me.
>
> ## This produces a logical vector, which will get converted to a numeric
> ## vector the first time a number is assigned to it.  That seems
> ## wasteful.
> x <- rep(NA, n)
>
> ## This does the conversion ahead of time but it's still creating a
> ## logical vector first, which seems wasteful.
> x <- as.numeric(rep(NA, n))
>
> ## This avoids type conversion but still involves two assignments for
> ## each element in the vector.
> x <- numeric(n)
> x[] <- NA
>
> ## This seems reasonable.
> x <- rep(as.numeric(NA), n)
>
> Comments?

My intuition would be to go with the third method (allocate a numeric
vector then assign NA to its contents) but I haven't tested the
different.  In fact, it would be difficult to see differences in, for
example, execution time unless n was very large.

This brings up a different question which is, why do you want to
consider this?  Are you striving for readability, for speed, for low
memory footprint, for "efficiency" in some other way?  When we were
programming in S on machines with 1 mips processors and a couple of
megabytes of memory, such considerations were important.  I'm not sure
they are quite as important now.




More information about the R-help mailing list