0

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?

bsyben
  • 1
  • 3
    You probably want to take a close look at Kevin's excellent answer here: https://stackoverflow.com/questions/26241085/rcpp-function-check-if-missing-value/26262984#26262984 – Dirk Eddelbuettel May 01 '21 at 23:21
  • Thanks, @DirkEddelbuettel, I follow your suggestions and read the post in the link. My understanding is that the first if condition `x[i] == NA_REAL` never worked as expected. The reason we can obtain correct answer is due to the second if condition in the first for loop. I commented out the first if condition with `x[i] == NA_REAL` and still get desired results. – bsyben May 02 '21 at 14:34

0 Answers0