[英]Detecting and omitting na values from a std vector in Rcpp
I have a std::vector; 我有一个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. 在检查其中是否有Na值之后(如果有的话,显然要删除Na值),需要对其元素进行汇总。 I have to do it in Rcpp. 我必须在Rcpp中进行。 Now, for a numeric vector in Rcpp (NumericVector); 现在,对于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”,它很容易得出如下总和:
x<- c(NaN,1,2)
res(x)
[1] 3
Now for a std::vector x; 现在为一个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. 您应该能够使用RcppHoney (也在CRAN上使用),它将Rcpp Sugar的向量化成语(与R一样具有向量化的NA测试)带到任何可迭代的容器中-因此也包括STL容器。
See eg the into vignette for this example of combining different vector types and classes into a single scalar exppression: 有关将不同的向量类型和类组合为单个标量表达式的示例,请参见例如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.