Using as.integer(NA) in the .C function

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Using as.integer(NA) in the .C function

cgenolin
Hi the list,
I am including some C code in a R program using the .C interface. I want to deal with NA values, but the result is strange:

--- 8< ------------ C code -----------
void hein(int *a, int *b, int* c){
  *c = (*a + *b);
}
--- 8< -----------------------------------
--- 8< ------------- R code -----------
> .C("hein",as.integer(NA),as.integer(1),as.integer(1),NAOK=TRUE)[[3]]
[1] -2147483647
--- 8< ---------------------------------
The result should be NA, isn't it? What wrong il my code?

Christophe
Reply | Threaded
Open this post in threaded view
|

Re: Using as.integer(NA) in the .C function

Berend Hasselman

On 07-05-2013, at 15:30, cgenolin <[hidden email]> wrote:

> Hi the list,
> I am including some C code in a R program using the .C interface. I want to
> deal with NA values, but the result is strange:
>
> --- 8< ------------ C code -----------
> void hein(int *a, int *b, int* c){
>  *c = (*a + *b);
> }
> --- 8< -----------------------------------
> --- 8< ------------- R code -----------
>> .C("hein",as.integer(NA),as.integer(1),as.integer(1),NAOK=TRUE)[[3]]
> [1] -2147483647
> --- 8< ---------------------------------
> The result should be NA, isn't it?

Why?

> What wrong il my code?


Read the manual "Writing R Extensions".
See section 5.10.3 and section 6.4 in that manual (I'm referring to the pdf available on CRAN for R-3.0.0).
There may be more references in the manual but these were the first I found by searching, which you should have done.

Berend

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Using as.integer(NA) in the .C function

cgenolin
Damm... I am reading the WRE, but I am only at page 83. I start to try to play with NAOK to early.

Anyway, exactly the same function for numeric instead of integer will give different results:

--- 8< ------------ C code -----------
void hein2(double *a, double *b, double* c){
*c = (*a + *b);
}
--- 8< -----------------------------------
--- 8< ------------- R code -----------
 .C("hein2",as.numeric(NA),as.numeric(1),as.numeric(1),NAOK=TRUE)[[3]]
[1] NA
--- 8< ---------------------------------
That's why I find the results of "hein" stranges...

Christophe