r语言 - 分组数据和多个变量的滚动平均值



我想得到两个变量和分组数据在't-1', 't-2 ', 't-3'等时刻的滚动平均值。下面的代码不按变量londeglatdeg分组,变量vw和窗口为3天。如何让代码运行由longdeglatdeg分组的数据,并为变量uwvw计算?

浏览数据:

structure(list(dateday = structure(c(18525, 18525, 18525, 18525, 
18525, 18525, 18525, 18525, 18525, 18525, 18525, 18525, 18525, 
18525, 18525, 18525, 18525, 18525, 18525, 18525), class = "Date"), 
latdeg = c(39.875, 39.875, 39.875, 39.875, 39.875, 39.875, 
39.875, 39.875, 39.875, 39.875, 39.875, 39.875, 39.875, 39.875, 
39.875, 39.875, 39.875, 39.875, 39.875, 39.875), londeg = c(0.125, 
0.375, 0.625, 0.875, 1.125, 1.375, 1.625, 1.875, 2.125, 2.375, 
2.625, 2.875, 3.125, 3.375, 3.625, 3.875, 4.125, 4.375, 4.625, 
4.875), uw = c(-4.0046167, -2.9436386, -1.384588, -0.0981078, 
1.0286689, 1.91347, 2.4027817, 2.6116273, 2.7161477, 2.6292932, 
2.5535467, 1.9434952, 1.0219297, 0.6225892, 0.40092927, 0.5625489, 
1.1099254, 1.4549272, 1.716492, 2.1899667), vw = c(1.6297868, 
2.7284932, 3.7426023, 3.9648964, 3.8228495, 3.5595078, 3.2727604, 
2.994605, 2.795548, 2.652693, 1.9364153, 0.73406154, -0.4248537, 
-1.1846231, -1.6087885, -1.7109653, -1.2042828, -0.75072134, 
-0.56564194, -0.34468606)), row.names = c(NA, 20L), class = "data.frame")

代码(来自https://stackoverflow.com/a/64575069/14535832)

library(tidyverse)
library(slider)
df <- 1:3 %>%
map(~slide_dbl(df$vw, ~mean(.x), .before = .x, .complete = F)) %>%
bind_cols() %>% 
bind_cols(df, .) %>% 
set_names(c("dateday", "latdeg", "londeg", "uw", "vw_day0", paste0("vw_day", 1:3)))

我们可以在第一个map中添加一个group_split

library(dplyr)
library(purrr)
library(slider)
map(1:3, ~ df %>%
group_split(latdeg, londeg) %>%
map_dbl(function(x) slide_dbl(x$vw, ~ mean(.x, .before = .x,
.complete = FALSE))))

我找到了另一个答案来回答我的问题,这个答案可能有点迟钝,但效果很好。

for(rolldays in 1:3) {
new_col_name <- paste0("day_", rolldays)
df <- df %>% 
group_by(latdeg, londeg) %>% 
mutate(!!sym(new_col_name) := zoo::rollapplyr(uw, rolldays, mean, fill = NA, na.rm = T)) %>% 
as.data.frame()
}