有没有更好的方法来计算使用超前或滞后计算函数(平均值,总和等)的前3个时间段(或N个时间段)的平均值?
data_final <- data_final %>%
group_by(cars) %>%
mutate(last3_average = (lag(speed, 1) + lag(speed, 2) + lag(speed, 3))/3)
我使用了动物园包,如下所示:
data_final <- data_final %>%
group_by(cars) %>%
mutate(last3_average = lag(rollmean(speed, 3, na.pad = T, align = "right"), 1))