2016-06-12 36 views
6

我想查找数据帧中以前的n行的总和。例如:查找数据帧中前n行的总和

id = 1:10 
vals = c(4,7,2,9,7,0,4,6,1,8) 
test = data.frame(id,vals) 

所以,对于n=3,我要计算下一列:

test$valprevious = c(NA, head(test$vals,-1) 

test$sum = c(NA, NA, 13,18,18,16,11,10,11,15) 

我来创造使用新列最接近

然后使用循环遍历列重复此n次,然后sum。我相信这不是最有效的方法,是否有任何访问前一行的函数?或者另一种方式来做到这一点?

回答

11

可以使用rollsumr函数从zoo包这样的:

library(zoo) 
test$sums <- rollsumr(test$vals, k = 3, fill = NA) 

这给:

> 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 

这是与使用rollsum功能与align = 'right'参数:

rollsum(test$vals, k = 3, fill = NA, align = 'right') 

作为替代方案,可以使用Reduceshiftdata.table包:

library(data.table) 
setDT(test)[, sums := Reduce(`+`, shift(vals, 0:2))] 

可以得到相同的结果:

> 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 

提议@一个很好的基础R替代alexis_laz在评论中:

n <- 3 
cs <- cumsum(test$vals) 
test$sums <- c(rep_len(NA, n - 1), tail(cs, -(n - 1)) - c(0, head(cs, -n))) 

如在评论中提出@Khashaa另外两个选项:

# 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) 

基准:

由于@alexis_laz在评论中指出的,一些解决方案可能会在重新计算创建开销总和和重新创建length - 的传单。这可能会导致计算速度的差异。

# 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))] 

运行在小例子集基准:

library(microbenchmark) 
microbenchmark(alexis_laz(test), 
       khashaa(test), 
       rcpp_roll(test), 
       zoo_roll(test), 
       dt_reduce(test), 
       times = 10) 

这给:

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 

正如你可以看到RcppRoll解决方案和@Alexis_laz两个基础R解决方案和@Khashaa比zoodata.table解决方案快得多(但仍然在几微秒内,所以不用担心)。

有了一个更大的数据集:

test <- data.frame(id=rep(1:10,1e7), vals=sample(c(4,7,2,9,7,0,4,6,1,8),1e7,TRUE)) 

的情况发生了变化:

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 

现在RcppRoll解决方案显然是最快其次是data.table解决方案。

+1

另一种方法是避免重新计算'sum'并创建'length(vals)'向量,可能是'n = 3; cs = cumsum(test $ vals); c(0,head(cs,-n)))' –

+0

@alexis_laz Thx!这是一个非常好的基础R选择。将它添加到答案中。 – Jaap

+1

'rowSums(embed(test $ vals,3))'曾经是''RcppRoll'前期最有效的''。 – Khashaa