単純な Rcpp 実装でパフォーマンスを少し改善できます。
library(Rcpp)
library(microbenchmark)
withinR <- function(x,y) x >= y[1] & x <= y[2]
cppFunction("LogicalVector withinCpp(const NumericVector& x, const NumericVector& y) {
double min = y[0], max = y[1];
int n = x.size();
LogicalVector out(n);
for(int i = 0; i < n; ++i) {
double val = x[i];
if (NumericVector::is_na(val)) {
out[i] = NA_LOGICAL;
} else {
out[i] = val >= min & val <= max;
}
}
return out;
}")
x <- sample(100, 1e5, rep = T)
stopifnot(all.equal(withinR(x, c(25, 50)), withinCpp(x, c(25, 50))))
microbenchmark(
withinR(x, c(25, 50)),
withinCpp(x, c(25, 50))
)
C++ バージョンは、私のコンピューターでは約 4 倍高速です。より多くの Rcpp トリックを使用したい場合は、おそらくさらに微調整できますが、これはすでにかなり高速に思えます。R バージョンでさえ、ボトルネックになる前に非常に頻繁に呼び出す必要があります。
# Unit: microseconds
# expr min lq median uq max
# 1 withinCpp(x, c(25, 50)) 635 659 678 1012 27385
# 2 withinR(x, c(25, 50)) 1969 2031 2573 2954 4082