繁体   English   中英

从Rcpp中的std向量检测和忽略na值

[英]Detecting and omitting na values from a std vector in Rcpp

我有一个std :: vector; 在检查其中是否有Na值之后(如果有的话,显然要删除Na值),需要对其元素进行汇总。 我必须在Rcpp中进行。 现在,对于Rcpp中的数值向量(NumericVector); 如代码所示,这非常简单:

    cppFunction("
       double res ( NumericVector x){
         NumericVector v = x[! is_na(x)];
         return sum(v);
        }
        ")

因此,对于向量“ x”,它很容易得出如下总和:

       x<- c(NaN,1,2)
       res(x)
       [1] 3

现在为一个std :: vector x; 我该怎么做?

您应该能够使用RcppHoney (也在CRAN上使用),它将Rcpp Sugar的向量化成语(与R一样具有向量化的NA测试)带到任何可迭代的容器中-因此也包括STL容器。

有关将不同的向量类型和类组合为单个标量表达式的示例,请参见例如into vignette

// [[Rcpp::export]]
Rcpp::NumericVector example_manually_hooked() {

    // We manually hooked std::list in to RcppHoney so we'll create one
    std::list< int > l;
    l.push_back(1); l.push_back(2); l.push_back(3); l.push_back(4); l.push_back(5);

    // std::vector is already hooked in to RcppHoney in default_hooks.hpp so
    // we'll create one of those too
    std::vector< int > v(l.begin(), l.end());

    // And for good measure, let's create an Rcpp::NumericVector which is
    // also hooked by default
    Rcpp::NumericVector v2(v.begin(), v.end());

    // Now do some weird operations incorporating std::vector, std::list,
    // Rcpp::NumericVector and some RcppHoney functions and return it.  The
    // return value will be equal to the following R snippet:
    //     v <- 1:5
    //     result <- 42 + v + v + log(v) - v - v + sqrt(v) + -v + 42

    // We can store our result in any of RcppHoney::LogicalVector,
    // RcppHoney::IntegerVector, or RcppHoney::NumericVector and simply return
    // it to R.  These classes inherit from their Rcpp counterparts and add a
    // new constructor.  The only copy of the data, in this case, is when we
    // assign our expression to retval.  Since it is then a "native" R type,
    // returning it is a shallow copy.  Alternatively we could write this as:
    //     return Rcpp::wrap(1 + v + RcppHoney::log(v) - v - 1
    //         + RcppHoney::sqrt(v) + -v2);

    RcppHoney::NumericVector retval
        =  42 + l + v + RcppHoney::log(v) - v - l + RcppHoney::sqrt(v) + -v2
            + 42;
    return retval;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM