简体   繁体   English

基于条件R的滚动平均值

[英]Rolling mean based on conditions R

Here is a brief description of my data: The first column is date by month, the second column is binary variable (0 or 1), the third column is stock return, so each month's stock return point to 1 or 0. I want to calculate the 12-month rolling mean return separately based on the second column (0 or 1). 这是我的数据的简要说明:第一列是按月的日期,第二列是二进制变量(0或1),第三列是股票收益,因此每个月的股票收益指向1或0。我想根据第二列(0或1)分别计算12个月的滚动平均值回报。 There will be different number of 0s and 1s in the 12-month rolling base. 12个月的滚动基准中将有不同的0和1。 There should be 2 outcome (mean_rolling_0, and mean_rolling_1). 应该有2个结果(mean_rolling_0和mean_rolling_1)。

Use rollmean() from the zoo package, and apply this per group with group_by() in dplyr. 使用zoo包中的rollmean() ,并在dplyr中的group_by()中将其应用于每个组。

Here's an example. 这是一个例子。 I'm guessing at your data structure, but it will also work for similar structures. 我正在猜测您的数据结构,但它也适用于类似的结构。

library(tidyverse)
library(zoo)

# sample data
d = tibble(a = 1:100,
       b = sample(c(0,1), 100, replace = T),
       c = a/10 + rnorm(100))

# compute rolling mean 
d2 = d %>%
    group_by(b) %>%
    mutate(roll = rollmean(c, 12, na.pad=TRUE, align="right"))

# plot to see the effect
ggplot(data = d2) + geom_line(aes(x = a, y = c, colour = factor(b))) +
    geom_line(aes(x = a, y = roll, colour = factor(b)), linetype = 'dashed')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM