我正在通过对另一列进行分组来处理列的滚动平均值。这些组是“PlayerName”,滚动平均值是“FDP”。这是我正在使用的测试数据:
PlayerName <- c("Player1", "Player1", "Player1", "Player1", "Player2", "Player2", "Player2", "Player3", "Player3", "Player3", "Player3", "Player3", "Player3")
Date <- c("2016-11-02", "2016-11-04", "2016-11-07", "2016-11-10", "2016-11-03", "2016-11-04", "2016-11-11", "2016-11-02", "2016-11-04", "2016-11-07", "2016-11-10", "2016-11-13", "2016-11-17")
FDP <- c(13, 12, 8, 20, 10, 11, 12, 35, 46, 24, 33, 40, 40)
DT <- data.table(PlayerName, Date, FDP)
我想添加第4列“RollAvg”,这是3日期滚动平均值。结果看起来像:
PlayerName Date FDP RollAvg
1: Player1 2016-11-02 13 NA
2: Player1 2016-11-04 12 NA
3: Player1 2016-11-07 8 11
4: Player1 2016-11-10 20 13.3
5: Player2 2016-11-03 10 NA
6: Player2 2016-11-04 11 NA
7: Player2 2016-11-11 12 11
8: Player3 2016-11-02 35 NA
9: Player3 2016-11-04 46 NA
10: Player3 2016-11-07 24 35
11: Player3 2016-11-10 33 34.3
12: Player3 2016-11-13 40 32.3
13: Player3 2016-11-17 40 37.6
如果RollAvg列有NA或者省略它们,对我来说无关紧要。
我已经尝试了很多不同的方法来做到这一点,我无法得到任何工作。我知道还有其他类似的帖子,但我无法得到他们正在做的工作的结构。我觉得有一些简单的单行方式用sapply和rollmean做到这一点,但我无法弄明白。
感谢您的帮助!
答案 0 :(得分:3)
正如其他几个SO posts建议的那样,您可以使用zoo
包的rollapply()
函数来解决此问题:
library(zoo)
library(dplyr)
PlayerName <- c("Player1", "Player1", "Player1", "Player1", "Player2", "Player2", "Player2", "Player3", "Player3", "Player3", "Player3", "Player3", "Player3")
Date <- c("2016-11-02", "2016-11-04", "2016-11-07", "2016-11-10", "2016-11-03", "2016-11-04", "2016-11-11", "2016-11-02", "2016-11-04", "2016-11-07", "2016-11-10", "2016-11-13", "2016-11-17")
FDP <- c(13, 12, 8, 20, 10, 11, 12, 35, 46, 24, 33, 40, 40)
data.frame(PlayerName, Date, FDP) %>%
mutate(Date = as.Date(Date)) %>%
arrange(PlayerName,Date) %>%
group_by(PlayerName) %>%
mutate(RollAvg = zoo::rollapply(FDP,3,mean,by = 1,align = "right",fill = NA))
返回:
Source: local data frame [13 x 4]
Groups: PlayerName [3]
PlayerName Date FDP RollAvg
<fctr> <date> <dbl> <dbl>
1 Player1 2016-11-02 13 NA
2 Player1 2016-11-04 12 NA
3 Player1 2016-11-07 8 11.00000
4 Player1 2016-11-10 20 13.33333
5 Player2 2016-11-03 10 NA
6 Player2 2016-11-04 11 NA
7 Player2 2016-11-11 12 11.00000
8 Player3 2016-11-02 35 NA
9 Player3 2016-11-04 46 NA
10 Player3 2016-11-07 24 35.00000
11 Player3 2016-11-10 33 34.33333
12 Player3 2016-11-13 40 32.33333
13 Player3 2016-11-17 40 37.66667