正如@Frank 所建议的那样,可以使用Rcpp
. 这是一个版本,其中包含一个受 Rcpp 启发的宏,dispatch.h
它处理所有原子向量类型:
mod_vector.cpp
#include <Rcpp.h>
using namespace Rcpp;
template <int RTYPE>
Vector<RTYPE> mod_vector_impl(Vector<RTYPE> x, IntegerVector i, Vector<RTYPE> value) {
if (i.size() != value.size()) {
stop("i and value must have same length.");
}
for (int a = 0; a < i.size(); a++) {
x[i[a] - 1] = value[a];
}
return x;
}
#define __MV_HANDLE_CASE__(__RTYPE__) case __RTYPE__ : return mod_vector_impl(Vector<__RTYPE__>(x), i, Vector<__RTYPE__>(value));
// [[Rcpp::export]]
SEXP mod_vector(SEXP x, IntegerVector i, SEXP value) {
switch(TYPEOF(x)) {
__MV_HANDLE_CASE__(INTSXP)
__MV_HANDLE_CASE__(REALSXP)
__MV_HANDLE_CASE__(RAWSXP)
__MV_HANDLE_CASE__(LGLSXP)
__MV_HANDLE_CASE__(CPLXSXP)
__MV_HANDLE_CASE__(STRSXP)
__MV_HANDLE_CASE__(VECSXP)
__MV_HANDLE_CASE__(EXPRSXP)
}
stop("Not supported.");
return x;
}
例子:
x <- 1:20
address(x)
#[1] "0x564e7e8"
mod_vector(x, 4:5, 12:13)
# [1] 1 2 3 12 13 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
address(x)
#[1] "0x564e7e8"
与 base 和 data.table 方法的比较。可以看出它的速度要快得多:
x <- 1:2e7
microbenchmark::microbenchmark(mod_vector(x, 4:5, 12:13), x[4:5] <- 12:13, modify.vector(x, 4:5, 12:13))
#Unit: microseconds
# expr min lq mean median uq
# mod_vector(x, 4:5, 12:13) 5.967 7.3480 15.05259 9.718 21.0135
# x[4:5] <- 12:13 2.953 5.3610 45722.61334 48122.996 52623.1505
# modify.vector(x, 4:5, 12:13) 954.577 988.7785 1177.17925 1021.380 1361.1210
# max neval
# 58.463 100
# 126978.146 100
# 1559.985 100