r - R 中的滚动计算,季度数据,但滚动应按年度计算,cumprod
问题描述
我有以下数据
PERIOD GROWTH PRICE
2011K1 0.88 0.88
2011K2 0.93 0.93
2011K3 0.96 0.96
2011K4 0.98 0.98
2012K1 1.13
2012K2 1.16
2012K3 1.12
2012K4 1.17
2013K1 1.07
2013K2 1.11
2013K3 1.03
2013K4 1.03
In 2011 PRICE = GROWTH
In 2012K1 PRICE = GROWTH[2012K1]*avg(PRICE in 2011)
In 2012K2 PRICE = GROWTH[2012K2]*avg(PRICE in 2011)
In 2012K3 PRICE = GROWTH[2012K3]*avg(PRICE in 2011)
In 2012K4 PRICE = GROWTH[2012K4]*avg(PRICE in 2011)
In 2013K1 PRICE = GROWTH[2013K1]*avg(PRICE in 2012)
In 2013K2 PRICE = GROWTH[2013K2]*avg(PRICE in 2012)
In 2013K3 PRICE = GROWTH[2013K3]*avg(PRICE in 2012)
In 2013K4 PRICE = GROWTH[2013K4]*avg(PRICE in 2012)
...
在每个季度中,上一季度的平均价格用于乘以该特定季度的增长,即同一年内的每个季度乘以相同的平均价格,即前一年的平均价格。
我尝试使用 cumprod() 但当我的数据为季度时未能使其每年滚动。我可以做for循环,问题是我必须为成千上万的产品做这个。
有什么建议么?
解决方案
-- 更新:意识到这个答案会产生不正确的结果 -- @Rebecca
另外的选择 :)
# I'll use tidyverse for this approach.
library(tidyverse)
# First, I'll generate a dataset similar to yours.
data <- tibble(year = rep(2011:2013, each=4),
quarter = rep(1:4, times=3),
growth_quarter = c(0.88,
0.93,
0.96,
0.98,
1.13,
1.16,
1.12,
1.17,
1.07,
1.11,
1.03,
1.03))
# Create a new tibble with desired output.
data_m <- data %>%
# Find the average growth per year.
group_by(year) %>%
mutate(growth_annual = mean(growth_quarter)) %>%
# Remove grouping by year for next calculations.
ungroup() %>%
# Organize by year and quarter to ensure consistent results for calculation in next step.
arrange(year, quarter) %>%
# Multiply current quarter's growth by last year's average growth.
mutate(growth_quarter*lag(growth_annual))
请让我知道,如果你有任何问题!
推荐阅读
- http-post - 如何使用 Indy10 Http 发布二进制 jpg 文件?
- python - nng 到 pynng 发布/订阅。客户未收到任何消息
- google-kubernetes-engine - 如何使用 Cloud Shell 从 GKE 中的集群角色绑定中删除主题
- excel - 使用 VBA 从 Excel 打开 Word – 几个错误
- c# - 如何在 C# 中解决 BinaryFormatter 反序列化第 3 方对象异常?
- c - 在 C 中将立体声 wav 转换为单声道
- php - 交集表的连接语句
- r - R中的data.table是否有更新的版本或替代品?
- r - 使用 RestRserve 将每个请求记录到单独的 json 文件
- arrays - 从json(Angular)中的对象数组中检索数据