I was working on the exercise of section 25.4 of "Advanced R" by Hardley from https://adv-r.hadley.nz/rcpp.html#rcpp-na. The question I try to replicate is to construct a function in Rcpp which mimics the generic R function min(x) with na.rm options. According to the exercise, the final answer, minC(x,na.rm), should have the exact same behaviors as min(x). The solution is as below from http://advanced-r-solutions.rbind.io/rewriting-r-code-in-c.html (Exercise 20.2):
NumericVector minC(NumericVector x, bool na_rm = false)
{
int n = x.size();
NumericVector out = NumericVector::create(R_PosInf);
if(na_rm){
for(int i = 0; i < n; ++i){
if(x[i]==NA_REAL){
continue;
}
if(x[i] < out[0]){
out[0] = x[i];
}
}
}else{
for(int i = 0; i < n; ++i){
if (NumericVector::is_na(x[i])){
out[0] = NA_REAL;
return out;
}
if(x[i] < out[0]){
out[0] = x[i];
}
}
}
return out;
}
I test the function and it works as expected:
x <- 1:10
min(x)
[1] 1
minC(x)
[1] 1
x[1] <- NA
min(x)
[1] NA
minC(x)
[1] NA
min(x,na.rm=T)
[1] 2
minC(x,na.rm=T)
[1] 2
However, if I change the second if condition from NumericVector::is_na(x[i])
to x[i]==NA_REAL
, the results become:
> x <- 1:10
> min(x)
[1] 1
> minC(x)
[1] 1
> x[1] <- NA
> min(x)
[1] NA
> minC(x)
[1] 2
> min(x,na.rm=T)
[1] 2
> minC(x,na_rm = T)
[1] 2
Can anyone help me understand why does the results changed after I make the change mentioned above?