根据每日时间序列数据逐周计算R增长率 [英] R growth rate calculation week over week on daily timeseries data
问题描述
我试图完全以R为单位计算w/w增长率.我可以使用excel或使用ruby进行预处理,但这不是重点.
I'm trying to calculate w/w growth rates entirely in R. I could use excel, or preprocess with ruby, but that's not the point.
data.frame示例
data.frame example
date gpv type
1 2013-04-01 12900 back office
2 2013-04-02 16232 back office
3 2013-04-03 10035 back office
我要根据类型"进行此操作,并且需要将日期类型"列包装成几周.然后计算逐周增长.
I want to do this factored by 'type' and I need to wrap up the Date type column into weeks. And then calculate the week over week growth.
我想我需要按周进行分组-使用自定义函数来确定日期是否在给定的一周内?
I think I need to do ddply to group by week - with a custom function that determines if a date is in a given week or not?
然后,在此之后,使用diff
并找到黑白周的增长除以前一周.
Then, after that, use diff
and find the growth b/w weeks divided by the previous week.
然后,我将绘制每周/每周的增长图,或使用data.frame导出它.
Then I'll plot week/week growths, or use a data.frame to export it.
已关闭,但有相同的有用想法.
This was closed but had same useful ideas.
推荐答案
对于最后一部分,如果要计算增长率,可以先获取日志,然后使用diff,其默认参数lag = 1(前一周)和差异= 1(第一个差异):
For the last part, if you want to calculate the growth rate you can take logs and then use diff, with the default parameters lag = 1 (previos week) and difference = 1 (first difference):
df_weekly_log <- log(df_weekly)
gr <- diff(df_weekly_log , lag = 1, differences = 1)
后者是一个近似值,对小的差异有效.
The later is an approximation, valid for small differences.
希望有帮助.
这篇关于根据每日时间序列数据逐周计算R增长率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!