最新版本犰狳 http://dirk.eddelbuettel.com/code/rcpp.armadillo.html具有识别有限值和非有限值索引的函数。
所以这段代码
#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]
// [[Rcpp::export]]
arma::uvec whichNA(arma::vec x) {
return arma::find_nonfinite(x);
}
/*** R
t1 <- c(1,2,NA,NA,3,4,1,NA,5)
whichNA(t1)
*/
产生您想要的答案(在 C/C++ 中对离一进行模块,因为它们是从零开始的):
R> sourceCpp("/tmp/uday.cpp")
R> t1 <- c(1,2,NA,NA,3,4,1,NA,5)
R> whichNA(t1)
[,1]
[1,] 2
[2,] 3
[3,] 7
R>
如果您首先创建要子集化的序列,Rcpp 也可以做到这一点:
// [[Rcpp::export]]
Rcpp::IntegerVector which2(Rcpp::NumericVector x) {
Rcpp::IntegerVector v = Rcpp::seq(0, x.size()-1);
return v[Rcpp::is_na(x)];
}
添加到上面的代码会产生:
R> which2(t1)
[1] 2 3 7
R>
逻辑子集在 Rcpp 中也有些新内容。