è possibile utilizzare la funzione di rollsumr
dal pacchetto zoo
per questo:
library(zoo)
test$sums <- rollsumr(test$vals, k = 3, fill = NA)
che dà:
> test
id vals sums
1 1 4 NA
2 2 7 NA
3 3 2 13
4 4 9 18
5 5 7 18
6 6 0 16
7 7 4 11
8 8 6 10
9 9 1 11
10 10 8 15
Questo è lo stesso che si utilizza la funzione rollsum
con il parametro align = 'right'
:
rollsum(test$vals, k = 3, fill = NA, align = 'right')
In alternativa, è possibile utilizzare Reduce
con shift
dal pacchetto data.table
:
library(data.table)
setDT(test)[, sums := Reduce(`+`, shift(vals, 0:2))]
che dà lo stesso risultato:
> test
id vals sums
1: 1 4 NA
2: 2 7 NA
3: 3 2 13
4: 4 9 18
5: 5 7 18
6: 6 0 16
7: 7 4 11
8: 8 6 10
9: 9 1 11
10: 10 8 15
Una bella alternativa di base R come proposto dal @ alexis_laz nei commenti:
n <- 3
cs <- cumsum(test$vals)
test$sums <- c(rep_len(NA, n - 1), tail(cs, -(n - 1)) - c(0, head(cs, -n)))
Un'altra opzione due come proposto dal @Khashaa nei commenti:
# with base R
n <- 3
test$sums <- c(rep_len(NA, n - 1), rowSums(embed(test$vals, n)))
# with RcppRoll
library(RcppRoll)
test$sums <- roll_sumr(test$vals, 3)
Benchmark:
Come @alexis_laz notato nei commenti, alcune delle soluzioni potrebbe creare in testa a ricalcolare somme e ricreazione length
-vectors. Ciò potrebbe causare differenze nella velocità di calcolo.
# creating function of the different solutions:
alexis_laz <- function(test) {n <- 3; cs <- cumsum(test$vals); test$sums <- c(rep_len(NA, n - 1), tail(cs, -(n - 1)) - c(0, head(cs, -n)))}
khashaa <- function(test) {n <- 3; test$sums <- c(rep_len(NA, n - 1), rowSums(embed(test$vals, n)))}
rcpp_roll <- function(test) test$sums <- roll_sumr(test$vals, 3)
zoo_roll <- function(test) test$sums <- rollsumr(test$vals, k=3, fill=NA)
dt_reduce <- function(test) setDT(test)[, sums := Reduce(`+`, shift(vals, 0:2))]
esecuzione il punto di riferimento sul piccolo esempio di dati:
library(microbenchmark)
microbenchmark(alexis_laz(test),
khashaa(test),
rcpp_roll(test),
zoo_roll(test),
dt_reduce(test),
times = 10)
che dà:
Unit: microseconds
expr min lq mean median uq max neval cld
alexis_laz(test) 61.390 99.507 107.7025 108.7515 122.849 131.376 10 a
khashaa(test) 35.758 92.596 94.1640 100.4875 103.264 112.779 10 a
rcpp_roll(test) 26.727 99.709 96.1154 106.1295 114.483 116.553 10 a
zoo_roll(test) 304.586 389.991 390.7553 398.8380 406.352 419.544 10 c
dt_reduce(test) 254.837 258.979 277.4706 264.0625 269.711 389.606 10 b
Come si può vedere la soluzione RcppRoll
e le due soluzioni di base R di @Alexis_laz e @Khashaa è notevolmente più veloce delle soluzioni zoo
e data.table
(ma ancora in microsecondi, quindi non è qualcosa di cui preoccuparsi).
Con un set di dati molto più grande:
test <- data.frame(id=rep(1:10,1e7), vals=sample(c(4,7,2,9,7,0,4,6,1,8),1e7,TRUE))
il quadro cambia:
Unit: milliseconds
expr min lq mean median uq max neval cld
alexis_laz(test) 3181.4270 3447.1210 4392.166 4801.410 4889.001 5002.363 10 b
khashaa(test) 6313.4829 7305.3334 7478.831 7680.176 7723.830 7859.335 10 c
rcpp_roll(test) 373.0379 380.9457 1286.687 1258.165 2062.388 2417.733 10 a
zoo_roll(test) 38731.0369 39457.2607 40566.126 40940.586 41114.990 42207.149 10 d
dt_reduce(test) 1887.9322 1916.8769 2128.567 2043.301 2218.635 2698.438 10 a
Ora la soluzione RcppRoll
è chiaramente il più veloce seguito dalla soluzione data.table
.
Un'alternativa, per evitare di ricalcolare 'sum's e creare' vettori 'length (vals)', potrebbe essere 'n = 3; cs = cumsum (test $ vals); c (rep_len (NA, n - 1), tail (cs, - (n - 1)) - c (0, head (cs, -n))) ' –
@alexis_laz Thx! Questa è un'alternativa di base R molto bella. Aggiunto alla risposta. – Jaap
'rowSums (embed (test $ vals, 3))' era il più efficiente nei giorni pre-'RcppRoll'. – Khashaa