質問
na.locf
(zoo
パッケージから)の既存のトリックがあるかどうかを確認したいと思いました。
私は実装するループを書きました。私は次のようにrle
の実装を行いました:
#include <Rcpp.h>
using namespace Rcpp;
//[[Rcpp::export]]
NumericVector naLocf(NumericVector x) {
int n=x.size();
for (int i=1;i<n;i++) {
if (R_IsNA(x[i]) & !R_IsNA(x[i-1])) {
x[i]=x[i-1];
}
}
return x;
}
.
私はただこれらが非常に基本的な機能なので、誰かがより良い方法で既にそれらをinverse.rle
でそれらを実装しているかもしれません(ループを回避するかもしれない)か速い方法は?
解決
私が言う唯一のことは、あなたがそれを一度だけそれをする必要がある場合に、各値に対してNA
を2回テストしているということです。NA
のテストは自由な操作ではありません。おそらくこのようなもの:
//[[Rcpp::export]]
NumericVector naLocf(NumericVector x) {
int n = x.size() ;
double v = x[0]
for( int i=1; i<n; i++){
if( NumericVector::is_na(x[i]) ) {
x[i] = v ;
} else {
v = x[i] ;
}
}
return x;
}
.
これはまだ不要なものをしています。私たちはこのようなものを試すことができます:
//[[Rcpp::export]]
NumericVector naLocf3(NumericVector x) {
double *p=x.begin(), *end = x.end() ;
double v = *p ; p++ ;
while( p < end ){
while( p<end && !NumericVector::is_na(*p) ) p++ ;
v = *(p-1) ;
while( p<end && NumericVector::is_na(*p) ) {
*p = v ;
p++ ;
}
}
return x;
}
.
今、私たちはいくつかのベンチマークを試すことができます:
x <- rnorm(1e6)
x[sample(1:1e6, 1000)] <- NA
require(microbenchmark)
microbenchmark( naLocf1(x), naLocf2(x), naLocf3(x) )
# Unit: milliseconds
# expr min lq median uq max neval
# naLocf1(x) 6.296135 6.323142 6.339132 6.354798 6.749864 100
# naLocf2(x) 4.097829 4.123418 4.139589 4.151527 4.266292 100
# naLocf3(x) 3.467858 3.486582 3.507802 3.521673 3.569041 100
. 所属していません StackOverflow