The values ​​of NA in the conditional expression Rcpp

I'm having problems with conditional expressions in Rcpp. The best way to explain my problem is with an example.

z <- seq(from=1,to=10,by=0.1) z[c(5,10,15,20,40,50,80)] <- NA src <- ' Rcpp::NumericVector vecz(z); for (int i=0;i<vecz.size();i++) { if (vecz[i] == NA_REAL) { std::cout << "Here is a missing value" << std::endl; } } ' func <- cxxfunction(signature(z="numeric"),src,plugin="Rcpp") func(z) # NULL 

From my understanding, NA_REAL represents NA values ​​for reals in Rcpp, and NA_Integer represents NA values ​​for integers. I'm not sure why the above condition never returns true given z .

+8
r rcpp
source share
1 answer

You might want to use the RC level function R_IsNA .

 require(Rcpp) require(inline) z <- seq(from=1, to=10, by=0.1) z[c(5, 10, 15, 20, 40, 50, 80)] <- NA src <- ' Rcpp::NumericVector vecz(z); for (int i=0; i< vecz.size(); i++) { if (R_IsNA(vecz[i])) { Rcpp::Rcout << "missing value at position " << i + 1 << std::endl; } } ' func <- cxxfunction(signature(z="numeric"), src, plugin="Rcpp") ## results func(z) missing value at position 5 missing value at position 10 missing value at position 15 missing value at position 20 missing value at position 40 missing value at position 50 missing value at position 80 NULL 
+13
source share

All Articles