我想用滚动窗口计算11个月的总和,使用每日数据。数据框是这样的。我有17年的每日数据,该文件中包含300多家公司
head(Rolling_window)
# A tibble: 6 x 5
date price1 price2 price3 price4
<dttm> <dbl> <dbl> <dbl> <dbl>
1 2000-01-04 72.27 6.63 2.38 2.98
2 2000-01-06 72.27 6.63 2.39 2.98
3 2000-01-11 72.27 6.63 2.41 2.98
4 2000-01-12 69.00 6.63 2.41 2.98
5 2000-01-13 68.67 6.63 2.41 2.98
6 2000-01-14 69.60 6.63 2.41 2.98
答案 0 :(得分:0)
如果我理解你,你可以使用这个解决方案:
library(tidyverse)
date <- c('2000-01-04','2000-01-06','2000-01-11','2000-01-12',
'2000-01-13','2000-01-14','2000-02-14','2001-02-14')
n <- NROW(date)
df <- tibble(date,price1 = rnorm(n),price2 = rnorm(n,0.1),price3 = rnorm(n,10),price4 = rnorm(n,5))
df %>%
mutate(date = as.character(date)) %>%
separate('date',c('year','month','day')) %>%
group_by(year,month) %>%
summarise_at(vars(price1, price2, price3, price4),funs(mean)) %>%
ungroup
快速更新
我使用data.table
包重写代码,现在它的工作速度会快得多:
require(data.table)
df <- as.data.table(df)
df_sum <- df[, c('year','month','day') := tstrsplit(date, "-", fixed=TRUE)
][
,c('date','day'):=NULL,
][
, lapply(.SD, mean), by = list(year,month)
][
, rollapply(.SD,11,sum,partial = T),
.SDcols=c('price1','price2','price3','price4')
]
答案 1 :(得分:0)
请在下面找到以下代码
library(dplyr)
library(zoo)
library(lubridate)
dummy = data.frame(Date = seq(as.Date("2016-01-01"),as.Date("2017-12-31"),"day"),
price1 = sample(5.5:9.5,731,replace = T),
price2 = sample(5.5:9.5,731,replace = T),
price3 = sample(5.5:9.5,731,replace = T),stringsAsFactors = F)
dummy_rolling_11m = dummy %>% mutate(yearmonth = as.yearmon(Date)) %>%
select(-Date)%>% group_by(yearmonth) %>%
summarise_all(funs(sum(.,na.rm = T))) %>%
arrange(yearmonth) %>% select(-yearmonth) %>%
mutate_all(funs(rollapplyr(.,11,sum,partial = T)))