我是C++和Rcpp的新手.假设,我有一个向量
t1<-c(1,2,NA,NA,3,4,1,NA,5)
Run Code Online (Sandbox Code Playgroud)
我想得到一个t1元素的索引NA.我可以写:
NumericVector retIdxNA(NumericVector x) {
// Step 1: get the positions of NA in the vector
LogicalVector y=is_na(x);
// Step 2: count the number of NA
int Cnt=0;
for (int i=0;i<x.size();i++) {
if (y[i]) {
Cnt++;
}
}
// Step 3: create an output matrix whose size is same as that of NA
// and return the answer
NumericVector retIdx(Cnt);
int Cnt1=0;
for (int i=0;i<x.size();i++) {
if (y[i]) {
retIdx[Cnt1]=i+1;
Cnt1++;
}
}
return retIdx;
}
Run Code Online (Sandbox Code Playgroud)
然后我明白了
retIdxNA(t1)
[1] 3 4 8
Run Code Online (Sandbox Code Playgroud)
我在想:
(i)whichRcpp中是否有任何等价物?
(ii)有没有办法让上述功能更短/更清脆?特别是,有没有简单的方法来结合上面的步骤1,2,3?
Dir*_*tel 12
最新版本的RcppArmadillo具有识别有限值和非有限值的指数的功能.
所以这段代码
#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)
*/
Run Code Online (Sandbox Code Playgroud)
得到你想要的答案(在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>
Run Code Online (Sandbox Code Playgroud)
如果您首先创建序列到子集,Rcpp也可以这样做:
// [[Rcpp::export]]
Rcpp::IntegerVector which2(Rcpp::NumericVector x) {
Rcpp::IntegerVector v = Rcpp::seq(0, x.size()-1);
return v[Rcpp::is_na(x)];
}
Run Code Online (Sandbox Code Playgroud)
添加到上面的代码产生:
R> which2(t1)
[1] 2 3 7
R>
Run Code Online (Sandbox Code Playgroud)
逻辑子集在Rcpp中也有些新功能.
尝试这个:
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
IntegerVector which4( NumericVector x) {
int nx = x.size();
std::vector<int> y;
y.reserve(nx);
for(int i = 0; i < nx; i++) {
if (R_IsNA(x[i])) y.push_back(i+1);
}
return wrap(y);
}
Run Code Online (Sandbox Code Playgroud)
我们可以在R中像这样运行:
> which4(t1)
[1] 3 4 8
Run Code Online (Sandbox Code Playgroud)
性能
请注意,我们已经更改了上述解决方案,为输出向量保留了空间。替换which3为:
// [[Rcpp::export]]
IntegerVector which3( NumericVector x) {
int nx = x.size();
IntegerVector y;
for(int i = 0; i < nx; i++) {
// if (internal::Rcpp_IsNA(x[i])) y.push_back(i+1);
if (R_IsNA(x[i])) y.push_back(i+1);
}
return y;
}
Run Code Online (Sandbox Code Playgroud)
那么,在9个元素长的向量上的性能which4最快的如下:
> library(rbenchmark)
> benchmark(retIdxNA(t1), whichNA(t1), which2(t1), which3(t1), which4(t1),
+ replications = 10000, order = "relative")[1:4]
test replications elapsed relative
5 which4(t1) 10000 0.14 1.000
4 which3(t1) 10000 0.16 1.143
1 retIdxNA(t1) 10000 0.17 1.214
2 whichNA(t1) 10000 0.17 1.214
3 which2(t1) 10000 0.25 1.786
Run Code Online (Sandbox Code Playgroud)
对于9000个矢量元素重复此操作,Armadillo解决方案的速度要比其他解决方案快很多。在这里which3(which4除了不为输出向量保留空间之外,其他都是相同的),最糟糕的which4是第二位。
> tt <- rep(t1, 1000)
> benchmark(retIdxNA(tt), whichNA(tt), which2(tt), which3(tt), which4(tt),
+ replications = 1000, order = "relative")[1:4]
test replications elapsed relative
2 whichNA(tt) 1000 0.09 1.000
5 which4(tt) 1000 0.79 8.778
3 which2(tt) 1000 1.03 11.444
1 retIdxNA(tt) 1000 1.19 13.222
4 which3(tt) 1000 23.58 262.000
Run Code Online (Sandbox Code Playgroud)