How to rollapply over a multi column data table

2019-03-02 05:08发布

I would like to use the rollapply function over a multi column datatable, namely I would like to be able to use each column independantly for instance let's consider the following datable :

> DT = data.table(x=rep(c("a","b","c"),each=3), y=c(1,3,6), v=1:9)
> DT
   x y v
1: a 1 1
2: a 3 2
3: a 6 3
4: b 1 4
5: b 3 5
6: b 6 6
7: c 1 7
8: c 3 8
9: c 6 9

Then I would like to use rollapply as a rolling subset in order to work out the rolling mean over 3 element of columns 2 and 3 and store them into external variables :

> r1= NA; r2 = NA
> ft=function(x) { r1=mean(x[,2,with=FALSE]) ; r2=mean(x[,3,with=FALSE]) }
> rollapply(DT, width=3, ft)
 Error in x[, 2, with = FALSE] : incorrect number of dimensions 

Except I got this error which isn't handy, why isn't it working ?

The output would be :

> r1
[1] 3.333333 3.333333 3.333333 3.333333 3.333333 3.333333 3.333333
> r2
[1] 2 3 4 5 6 7 8

2条回答
forever°为你锁心
2楼-- · 2019-03-02 05:21

Just to add another option using data.table only

library(data.table) # v1.9.6+

Define the rolling mean function

rollMean <- function(x, n) Reduce(`+`, shift(x, 0L:(n - 1L)))/n

Apply it on multiple columns while specifying .SDcols

DT[, lapply(.SD, rollMean, 3L), .SDcols = y:v]
#           y  v
# 1:       NA NA
# 2:       NA NA
# 3: 3.333333  2
# 4: 3.333333  3
# 5: 3.333333  4
# 6: 3.333333  5
# 7: 3.333333  6
# 8: 3.333333  7
# 9: 3.333333  8
查看更多
在下西门庆
3楼-- · 2019-03-02 05:42

You are almost there and can do:

lapply(DT[,2:3,with=F], function(x) rollapply(x,width=3, FUN=mean))
#$y
#[1] 3.333333 3.333333 3.333333 3.333333 3.333333 3.333333 3.333333

#$v
#[1] 2 3 4 5 6 7 8
查看更多
登录 后发表回答