简体   繁体   中英

Detecting and omitting na values from a std vector in Rcpp

I have a std::vector; whose elements need to be summed up after checking if there is any Na values (and obviously removing the Na values if it has any) in it. I have to do it in Rcpp. Now, for a numeric vector in Rcpp (NumericVector); it is very easy as the code says below:

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

. So for a vector "x", it easily gives the sum as follows:

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

Now for a std::vector x; how can I do the same?

You should be able to use RcppHoney (also on CRAN here ) which brings the vectorised idioms of Rcpp Sugar (which has vectorised NA tests just like R has) to any iterable container -- hence also STL ones.

See eg the into vignette for this example of combining different vector types and classes into a single scalar exppression:

// [[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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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