Home > OS >  Rolling averages for grouped data and more than one variable
Rolling averages for grouped data and more than one variable

Time:10-19

I would like to get the rolling average at time 't-1', 't-2, 't-3', etc. of two variables and grouped data. The code below works without grouping by variables londeg and latdeg, with variable vw and a window of 3 days. How can I get the code running for the data grouped by longdeg and latdeg, and calculated for the variables uw and vw?

Glance at data :

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")

Code (from 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)))

Lee

CodePudding user response:

We may do a group_split within the first map

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))))
  • Related