当基础时间序列为月频率时,如何从时间序列索引中获取年份?
我想从时间序列索引中提取年份(基础时间序列是每月频率)。我想要这样做的原因是创建一个年度轴,例如
plot(myts)
axis(1, at = year(time(myts)), labels = FALSE)
# note I know 'year()' does not work :)
因为如果我只是绘制它,R 会任意(?)创建一个时间轴。通常是两年甚至五年的轴,这有时是不合适的。
tsp(myts)
[1] 1966.000 1974.917 12.000
I´d like to extract years from a time series index (the underlying time series is of monthly frequency). The reason I want to do it is creating a yearly axis, e.g.
plot(myts)
axis(1, at = year(time(myts)), labels = FALSE)
# note I know 'year()' does not work :)
because if I just plot it, R arbitrarily(?) creates a time axis. Often it's a two or even 5 year axis which makes is inappropriate sometimes.
tsp(myts)
[1] 1966.000 1974.917 12.000
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我找到了自己的解决方案。也许这对其他人也有帮助。此外,我认为这不太聪明......所以我期待您的建议。
编辑:找到了一个更优雅的解决方案:
根据@matty T pain,它也适用于
ts
(请参阅评论)。I found an own solution. Maybe this helps someone else, too. Besides I think it's not overly smart... so I am looking forward to your suggestions.
EDIT: found a more elegant solution:
according to @matty T pain it also works for
ts
(see comments).我对你的数据不熟悉,但如果是时间序列类(ts),那么你可以使用window函数:
window(myts,start="*beginyear*", end="*endyear*")
如果您无法做到这一点,也许您可以使用此处的一些建议:
http://r.789695.n4 .nabble.com/Year-and-Month-extraction-from-Date-object-td904011.html
马特
I am not familiar with your data, but if it is a time series class (ts), then you can use the window function:
window(myts,start="*beginyear*",end="*endyear*")
If you cannot do that, maybe you can use some of the suggestions here:
http://r.789695.n4.nabble.com/Year-and-Month-extraction-from-Date-object-td904011.html
Matt