r中次要因素的移动平均数

moving average by secondary factor in r

我以此为例 (Calculating moving average),我已成功将其合并到我的代码中。我需要计算滚动平均值和滚动中位数(我已经完成了),但我的数据集很大,我需要添加一个辅助变量来过滤它。在下面的示例中,他们计算了 10 天数据集的滚动平均值。如果他们在不同地点有 10 天,我们需要根据这些不同地点计算 10 天的滚动均值,会发生什么情况?

library(tidyverse)
library(zoo)

some_data = tibble(day = 1:10)
# cma = centered moving average
# tma = trailing moving average
some_data = some_data %>%
mutate(roll_mean = rollmean(day, k = 3, fill = NA)) %>%
mutate(roll_median = rollmedian(day, k = 3, fill = NA, align = "right"))
some_data

您可以按位置分组:

library(tidyverse)
library(zoo)

some_data <- rbind(tibble(day = 1:5,location = c(rep("A",5))),
                   tibble(day = 1:5,location = c(rep("B",5))))

some_data <- some_data %>% group_by(location) %>%
  mutate(roll_mean_left = rollmean(day, k = 3, fill = NA, align='left'),
         roll_mean_right = rollmean(day, k = 3, fill = NA, align='center'),
         roll_median_center = rollmedian(day, k = 3, fill = NA, align = 'right'))

some_data

滚动功能针对每个位置重新初始化。
注意滚动window如何根据align参数移动:

     day location roll_mean_left roll_mean_right roll_median_center
   <int> <chr>             <dbl>           <dbl>              <dbl>
 1     1 A                     2              NA                 NA
 2     2 A                     3               2                 NA
 3     3 A                     4               3                  2
 4     4 A                    NA               4                  3
 5     5 A                    NA              NA                  4
 6     1 B                     2              NA                 NA
 7     2 B                     3               2                 NA
 8     3 B                     4               3                  2
 9     4 B                    NA               4                  3
10     5 B                    NA              NA                  4