R:一段时期(月)的左侧移动平均线
我有一个问题对于你们大多数人来说可能是微不足道的。我尝试了很多,但没有找到解决方案,所以如果有人能给我提示,我会很高兴。起点是每周的 xts 时间序列。
Month Week Value Goal Dec 2011 W50 a a Dec 2011 W51 b mean(a,b) Dec 2011 W52 c mean(a,b,c) Dec 2011 W53 d mean(a,b,c,d) Jan 2012 W01 e e Jan 2012 W02 f mean(e,f) Jan 2012 W03 g mean(e,f,g) Jan 2012 W04 h mean(e,f,g,h) Feb 2012 W05 i i Feb 2012 W06 j mean(i,j)
请原谅 Excel 符号,但我认为它非常清楚地说明了我想要做什么:我想计算“值”列的左侧移动平均值,但仅计算相应月份的左侧移动平均值,因为它显示在“目标”列中。我尝试了 apply.monthly() 和 period.apply()。但它并没有得到我想要的。你们中有人能给我一个如何解决这个问题的提示吗?只需提示我应该使用哪个函数就足够了!
非常感谢!
最好的问候,
安德烈亚斯
I have a question which might be trivial for most of you guys. I tried a lot, didn't come to a solution, so I would be glad if somebody could give me a hint. The starting point is a weekly xts-time series.
Month Week Value Goal Dec 2011 W50 a a Dec 2011 W51 b mean(a,b) Dec 2011 W52 c mean(a,b,c) Dec 2011 W53 d mean(a,b,c,d) Jan 2012 W01 e e Jan 2012 W02 f mean(e,f) Jan 2012 W03 g mean(e,f,g) Jan 2012 W04 h mean(e,f,g,h) Feb 2012 W05 i i Feb 2012 W06 j mean(i,j)
Please excuse the Excel notation, but I think it makes it pretty clear what I want to do: I want to calculate a left sided moving average for the column "Value" but just for the respective month, as it is displayed in the column Goal. I experimented with apply.monthly() and period.apply(). But it didn't get me what I want. Can sombody of you give me a hint how to solve the problem? Just a hint which function I should use would be already enough!
Thank you very much!
Best regards,
Andreas
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
apply.monthly
不起作用,因为它只为周期的终点分配一个值,而您希望为每个月周期分配多个值。您可以很容易地做到这一点,只需按月分割 xts 数据,对每个数据应用累积平均值函数,然后将列表重新绑定在一起。
apply.monthly
will not work because it only assigns one value to the endpoint of the period, whereas you want to assign many values to each monthly period.You can do this pretty easily by splitting your xts data by month, applying a cumulative mean function to each, and rbind'ing the list back together.
我希望我答对了你的问题。但这是您正在寻找的吗:
这应该可行 - 尽管 可重现的示例 会很好。
这就是它的作用。
结果:
当然,您可以通过帮助获取更多信息:
?ddply
或?apply.fromstart
。I hope I got your question right. but is it that what you are looking for:
this should work - though a reproducible expample would have been nice.
here's what it does.
outcome:
of course you may obtain further info via the help:
?ddply
or?apply.fromstart
.