带有一些基准测试的RcppRoll 解决方案:
library(RcppRoll)
#> Warning: package 'RcppRoll' was built under R version 4.1.2
v <- as.numeric(1:12e4)
n <- 3L
fRoll <- function(v, n) roll_sum(v, n, by = n)
fMatrix <- function(v, n) colSums(matrix(v, nrow = n))
fCumsum <- function(v, n) diff(c(0, cumsum(v))[seq(1, length(v) + 1L, n)])
fReduce <- function(v, n) Reduce(`+`, split(v, 1:n))
fLoop <- function(v, n) {
vOut <- numeric(ceiling(length(v)/n))
idx <- 0:(n - 1)
for (i in seq_along(vOut)) {
vOut[i] <- sum(v[i*n - idx])
}
return(vOut)
}
fApply1 <- function(v, n) tapply(v, cumsum(seq_along(v) %% n == 1), sum)
fApply2 <- function(v, n) tapply(v, 0:(length(v) - 1) %/% n, sum)
microbenchmark::microbenchmark(fRoll(v, n), fMatrix(v, n), fCumsum(v, n), fReduce(v, n), fLoop(v, n), fApply1(v, n), fApply2(v, n))
#> Unit: microseconds
#> expr min lq mean median uq max neval
#> fRoll(v, n) 145.7 215.45 290.512 234.35 284.05 1559.6 100
#> fMatrix(v, n) 282.0 360.85 428.385 382.20 437.70 1678.0 100
#> fCumsum(v, n) 1724.5 1805.05 2060.008 1859.15 2094.30 6169.5 100
#> fReduce(v, n) 1852.5 1943.15 2056.358 1985.75 2096.15 4335.6 100
#> fLoop(v, n) 19976.5 22618.05 25725.492 23860.95 25300.10 73618.3 100
#> fApply1(v, n) 69336.2 73841.35 77741.583 76639.80 80791.70 123253.8 100
#> fApply2(v, n) 69178.3 73870.80 77691.152 76582.65 79066.60 101159.1 100
# check that all the functions return the same result
results <- lapply(list(fRoll, fMatrix, fCumsum, fReduce, fLoop, fApply1, fApply2), function(f) as.numeric(f(v, n)))
sum(duplicated.default(results)) == length(results) - 1L
#> [1] TRUE
Created on 2021-12-02 by the reprex package (v2.0.1)
可能不会在 base r 中击败 colSums(matrix(。