[Rcpp-devel] Missing values

Romain Francois romain at r-enthusiasts.com
Fri Nov 16 14:04:14 CET 2012


Le 16/11/12 14:00, Hadley Wickham a écrit :
> On Fri, Nov 16, 2012 at 2:25 AM, Romain Francois
> <romain at r-enthusiasts.com> wrote:
>> Thanks for exploring these issue. This looks very useful.
>>
>> I get:
>>
>>> str( first_log(NA) )
>>   logi TRUE
>>> str( first_int(NA_integer_) )
>>   int NA
>>> str( first_num(NA_real_) )
>>   num NA
>>> str( first_char(NA_character_) )
>>   chr "NA"
>>
>>
>> For first_log: a bool can either be true or false. In R logical vectors are
>> represented as arrays of ints. When we coerce to bool, we test whether the
>> value is not 0. This works for most cases. I guess conversion to bool should
>> be avoided.
>
> There's obviously no perfect solution, and that seems to be consistent
> with C++'s treatment of NAs in logical expression: evalCpp("NAN ||
> FALSE") == TRUE
>
>> We have the is_na template function that can help:
>>
>>> evalCpp( 'traits::is_na<LGLSXP>( NA_LOGICAL )' )
>> [1] TRUE
>>> evalCpp( 'traits::is_na<REALSXP>( NA_REAL )' )
>> [1] TRUE
>>
>> And from this I can see we don't have is_na<STRSXP>, will fix this.
>>
>>> str( evalCpp( 'traits::get_na<REALSXP>()' ) )
>>   num NA
>>> str( evalCpp( 'traits::get_na<INTSXP>()' ) )
>>   int NA
>>
>> I guess we could come up with a nicer syntax for these, maybe static
>> functions in Vector<> so that we could do :
>>
>> IntegerVector::is_na( )
>> NumericVector::get_na( )
>
> Yes, that would be very nice.

Done.

>>> str( evalCpp( 'std::numeric_limits<int>::min()' ) )
>>   int NA
>>
>> This is how NA_integer_ is represented.
>
> Thanks - the danger is that you might do something like:
>
> evalCpp( 'traits::get_na<INTSXP>() + 1' )
>
> I'll make a note.

Yes. NA are particularly annoying in integer vector.

>> As said above, I'll add
>>
>> ...Vector::is_na
>> ...Vector::get_na
>>
>> to have something more consistent and not as cryptic as traits::is_na<...>(
>> ). People should not need to know what REALSXP, INTSXP, LGLSXP, ... mean.
>
> Great, thanks!
>
> Hadley
>


-- 
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30

R Graph Gallery: http://gallery.r-enthusiasts.com
`- http://bit.ly/SweN1Z : SuperStorm Sandy

blog:            http://romainfrancois.blog.free.fr
|- http://bit.ly/RE6sYH : OOP with Rcpp modules
`- http://bit.ly/Thw7IK : Rcpp modules more flexible



More information about the Rcpp-devel mailing list