通过RCpp返回NA

通过RCpp返回NA,r,rcpp,R,Rcpp,新手RCpp问题:我如何使数值向量返回到R?例如,假设我有一个RCpp代码,它将NA分配给向量的第一个元素 // [[RCpp::export]] NumericVector myFunc(NumericVector x) { NumericVector y=clone(x); y[0]=NA; // <----- what's the right expression here? return y; } /[[RCpp::export]] 数值向量myFunc(数值向

新手RCpp问题:我如何使数值向量返回到R?例如,假设我有一个RCpp代码,它将
NA
分配给向量的第一个元素

// [[RCpp::export]]
NumericVector myFunc(NumericVector x) {
   NumericVector y=clone(x);
   y[0]=NA; // <----- what's the right expression here?
   return y;
}
/[[RCpp::export]]
数值向量myFunc(数值向量x){
数值向量y=克隆(x);

y[0]=NA;//请至少尝试通过我们回归测试提供的大量示例进行grep:

edd@max:~$ cd  /usr/local/lib/R/site-library/Rcpp/unitTests/cpp/
edd@max:/usr/local/lib/R/site-library/Rcpp/unitTests/cpp$ grep NA *cpp | tail -5
support.cpp:           Rf_pentagamma( NA_REAL) ,
support.cpp:           expm1( NA_REAL ),
support.cpp:           log1p( NA_REAL ),
support.cpp:           Rcpp::internal::factorial( NA_REAL ),
support.cpp:           Rcpp::internal::lfactorial( NA_REAL )
edd@max:/usr/local/lib/R/site-library/Rcpp/unitTests/cpp$ 
此外,这实际上是R的一个C问题,并在编写R扩展手册中得到了回答


您也可以在中找到一个很好的例子,以及其他例子;在中有一个搜索函数。

为给定的向量类型(
NumericVector
IntegerVector
,…)获取正确的
NA
的标准方法是使用静态
get\NA
方法。类似于:

y[0] = NumericVector::get_na() ;

FWIW,您的代码与
Rcpp11
一样工作,它知道如何将静态
Na_代理
instance
Na
转换为目标类型的正确缺失值。

顺便说一句,在Rcpp中具有相同的行为很简单。谢谢,这很好。这可能是一种较短的编写方式:
numericy=no_init(n);for(int i=0;i
get_na
为您提供一个值。例如,如果您想要大小为na的向量,可以使用
NumericVector y=rep(NumericVector::get_na(),n)
NumericVector y(n,NumericVector::get_na());