当基础时间序列为每月频率时,如何从时间序列指数中获取年份? [英] How to get years from a time series index when the underlying time series is of monthly freq?

查看:55
本文介绍了当基础时间序列为每月频率时,如何从时间序列指数中获取年份?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从时间序列索引中提取年份(基础时间序列是每月频率).我想这样做的原因是创建一个年轴,例如

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 :)

因为如果我只是绘制它,R 会任意(?)创建一个时间轴.通常它是一个两年甚至 5 年的轴,这有时是不合适的.

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

推荐答案

我找到了自己的解决方案.也许这对其他人也有帮助.另外我觉得这还不算太聪明……所以我很期待你的建议.

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.

 axis(1, at = start(time(myts))[1]:end(time(myts))[1], labels = TRUE)

找到了一个更优雅的解决方案:

found a more elegant solution:

require(zoo)
x <- as.yearqtr("1991 Q1")
format.Date(x,"%Y")

根据@matty T pain 它也适用于 ts(见评论).

according to @matty T pain it also works for ts (see comments).

这篇关于当基础时间序列为每月频率时,如何从时间序列指数中获取年份?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆