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

时间:2019-01-10 11:16:28

标签: c++ r 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;我该怎么做?

1 个答案:

答案 0 :(得分:4)

您应该能够使用RcppHoney(也在CRAN here上使用)将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;
}