简体   繁体   English

R和Data.table-在多个列上应用rollapply

[英]R and Data.table - applying rollapply over multiple columns

I would really appreciate if you can help me do the rollapply for each column of the data.table 如果您能帮助我对data.table的每一列进行汇总,我将不胜感激。

    time                AUD NZD   EUR GBP    USD AUD 

1   2013-01-01 20:00    0.213     -0.30467   -0.127515 
2   2013-01-01 20:05    0.21191   -0.30467   -0.127975 
3   2013-01-01 20:10    0.212185  -0.304965  -0.127935 
4   2013-01-01 20:15    0.212055  -0.30511   -0.1288 
5   2013-01-01 20:20    0.211225  -0.30536   -0.12938 
6   2013-01-01 20:25    0.211185  -0.30527   -0.129195 
7   2013-01-01 20:30    0.21159   -0.3059    -0.13043 
8   2013-01-01 20:35    0.21142   -0.304955  -0.13155 
9   2013-01-01 20:40    0.21093   -0.30419   -0.132715 
10  2013-01-01 20:45    0.2078    -0.30339   -0.13544
11  2013-01-01 20:50    0.208445  -0.30304   -0.135645
12  2013-01-01 20:55    0.208735  -0.30185   -0.1357 
13  2013-01-01 21:00    0.20891   -0.303265  -0.13722 
14  2013-01-01 21:05    0.20903   -0.30428   -0.137495
15  2013-01-01 21:10    0.209615  -0.305495  -0.13734 
16  2013-01-01 21:15    0.20981   -0.30588   -0.13772 
17  2013-01-01 21:20    0.209855  -0.306935  -0.13801
18  2013-01-01 21:25    0.209585  -0.30604   -0.138045 
19  2013-01-01 21:30    0.210105  -0.3061    -0.137765 
20  2013-01-01 21:35    0.210335  -0.30734   -0.138525 

Code that works: 起作用的代码:

library("zoo")
library("data.table")

calculateAverage <- function (x,N) {
        tempDataStorage <- rollapply(out[,1], N, mean)
}

col1 <- out[,2]
col2 <- out[,3]
col3 <- out[,4]

average1 <- calculateAverage(col1, 2)
average2 <- calculateAverage(col2, 2)
average3 <- calculateAverage(col3, 2)

combine <- cbind(average1, average2, average3)
tempMatrix <- matrix(, nrow = nrow(out), ncol = ncol(out))
tempMatrix[2:nrow(out), 1:3] <- combine

Suggestion from SO : SO的建议

test <- lapply(out[,with=F], function(x) rollapply(x,width=2, FUN=mean))

Challenges: 1. The code I created works, but it feels inefficient and not generic. 挑战:1.我创建的代码可以正常工作,但是感觉效率低下并且不是通用的。 It needs to be modified whenever the number of cols changes 2. Suggestion from SO output is list which is not useful to me 每当cols数量更改时,都需要对其进行修改。2. SO输出的建议是列表,对我没有用

If an alternate method is suggested, I would be really appreciate it! 如果建议使用其他方法,我将不胜感激!

Thanks in advance Edit: Data table added 在此先感谢编辑:数据表已添加

data <- cbind(mtcars,as.Date(c("2007-06-22", "2004-02-13")))
merge(rollapply(Filter(is.numeric, data), 2, mean),
      Filter(Negate(is.numeric), data))

The first line creates data, so that there are not only numeric values in it. 第一行创建数据,因此其中不仅包含数字值。 This is only to mimic your data, which is not available right now. 这只是为了模仿您的数据,目前无法使用。

The second line filters only numeric columns and applies mean function to each of filtered columns. 第二行仅过滤数字列,并将mean函数应用于每个过滤的列。

Suggestion from David Arenburg worked perfectly! David Arenburg的建议非常有效!

MaPrice <- function(x, N) {
    Mavg <- rollapply(x, N, mean)
    Mavg
}

SpreadMA <- out[, lapply(.SD, MaPrice, N = 20)]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM