Sot*_*tos 9 performance r rcpp
我想基于元素之和小于或等于对矢量进行分组n
.假设如下,
set.seed(1)
x <- sample(10, 20, replace = TRUE)
#[1] 3 4 6 10 3 9 10 7 7 1 3 2 7 4 8 5 8 10 4 8
#Where,
n = 15
Run Code Online (Sandbox Code Playgroud)
预期输出将是组值,而它们的总和<= 15,即
y <- c(1, 1, 1, 2, 2, 3, 4, 5 ,5, 5, 6, 6, 6, 7, 7, 8, 8, 9, 9, 10)
Run Code Online (Sandbox Code Playgroud)
如你所见,总和绝不会超过15,
sapply(split(x, y), sum)
# 1 2 3 4 5 6 7 8 9 10
#13 13 9 10 15 12 12 13 14 8
Run Code Online (Sandbox Code Playgroud)
注意:我将在大型数据集(通常> 150 - 200GB)上运行它,因此效率是必须的.
我试过并接近但失败的方法是,
as.integer(cut(cumsum(x), breaks = seq(0, max(cumsum(x)) + 15, 15)))
#[1] 1 1 1 2 2 3 3 4 4 4 5 5 5 6 6 6 7 8 8 8
Run Code Online (Sandbox Code Playgroud)
这是我的Rcpp
解决方案(接近Khashaa 的解决方案,但更短/精简),因为你说速度很重要,Rcpp
可能是要走的路:
# create the data
set.seed(1)
x <- sample(10, 20, replace = TRUE)
y <- c(1, 1, 1, 2, 2, 3, 4, 5 ,5, 5, 6, 6, 6, 7, 7, 8, 8, 9, 9, 10)
# create the Rcpp function
library(Rcpp)
cppFunction('
IntegerVector sotosGroup(NumericVector x, int cutoff) {
IntegerVector groupVec (x.size());
int group = 1;
double runSum = 0;
for (int i = 0; i < x.size(); i++) {
runSum += x[i];
if (runSum > cutoff) {
group++;
runSum = x[i];
}
groupVec[i] = group;
}
return groupVec;
}
')
# use the function as usual
y_cpp <- sotosGroup(x, 15)
sapply(split(x, y_cpp), sum)
#> 1 2 3 4 5 6 7 8 9 10
#> 13 13 9 10 15 12 12 13 14 8
all.equal(y, y_cpp)
#> [1] TRUE
Run Code Online (Sandbox Code Playgroud)
如果有人需要相信速度:
# Speed Benchmarks
library(data.table)
library(microbenchmark)
dt <- data.table(x)
frank <- function(DT, n = 15) {
DT[, xc := cumsum(x)]
b = DT[.(shift(xc, fill=0) + n + 1), on=.(xc), roll=-Inf, which=TRUE]
z = 1; res = z
while (!is.na(z))
res <- c(res, z <- b[z])
DT[, g := cumsum(.I %in% res)][]
}
microbenchmark(
frank(dt),
sotosGroup(x, 15),
times = 100
)
#> Unit: microseconds
#> expr min lq mean median uq max neval cld
#> frank(dt) 1720.589 1831.320 2148.83096 1878.0725 1981.576 13728.830 100 b
#> sotosGroup(x, 15) 2.595 3.962 6.47038 7.5035 8.290 11.579 100 a
Run Code Online (Sandbox Code Playgroud)