NA values in Rcpp conditional

北慕城南 提交于 2019-12-04 02:36:28

You might want to use the R C 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
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!