[R] Best way to preallocate numeric NA array?

Henrique Dallazuanna wwwhsd at gmail.com
Thu Nov 26 17:32:09 CET 2009


Or best:

rep(NA_real_, 10)

On Thu, Nov 26, 2009 at 2:31 PM, Henrique Dallazuanna <wwwhsd at gmail.com> wrote:
> You can try this also:
>
> rep(NA_integer_, 10)
>
> On Thu, Nov 26, 2009 at 2:03 PM, 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?
>>
>> Thanks,
>> Rob
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
>
>
> --
> Henrique Dallazuanna
> Curitiba-Paraná-Brasil
> 25° 25' 40" S 49° 16' 22" O
>



-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O




More information about the R-help mailing list