【问题标题】:Simple time interval in RR中的简单时间间隔
【发布时间】:2013-12-09 21:32:28
【问题描述】:

我有一个具有以下属性的 ts:

summary(Y$Date)
                Min.               1st Qu.                Median                  Mean               3rd Qu.                  Max. 
"2001-01-01 05:30:00" "2004-03-15 10:40:30" "2007-01-03 04:00:00" "2006-11-11 15:53:11" "2009-08-13 12:00:00" "2011-12-30 12:30:00" 

str(Y$Date)
POSIXlt[1:174110], format: "2001-01-01 12:00:00" "2001-01-01 05:30:00" "2001-01-02 01:30:00" "2001-01-02 02:00:00" "2001-01-02 02:00:00" "2001-01-02 02:01:00" "2001-01-02 04:00:00" "2001-01-02 04:00:00" ...

length(Y$Date)
[1] 174110

Y$Date[1:5] 
[1] "2001-01-01 12:00:00" "2001-01-01 05:30:00" "2001-01-02 01:30:00" "2001-01-02 02:00:00" "2001-01-02 02:00:00"

我正在寻找一种创建时间间隔的简单方法,该时间间隔与以下结果相同:

Y$Date[grep("2001",Y$Date)]

从 2001 年提取所有值,仅采用以下形式:

Y$Date["2001" =< Y$Date < "2002"] #or
Y$Date["2001" =< Y$Date & Y$Date < "2002"] #for that matter.

因为它可以让我设置时间间隔,例如从 2001 年到 2005 年等。

打字:

Y$Date[Y$Date < "2002"] 

导致 R 重新调整整个系列。我已经使用 cut() 将 ts 分成几个间隔:

y<-cut(Y$Date,breaks="year") #and
as.data.frame(table(y))

两者都做得很好,但确实为我提供了我想要的灵活性。

非常感谢您的帮助,如果我能以任何方式帮助您解决此问题,请告诉我。

非常感谢。

【问题讨论】:

    标签: r time intervals


    【解决方案1】:

    将日期转换为xts类对象看library(xts)?xts的例子:

    date <- as.xts(Y$Date, descr='my new xts object')
    

    现在您可以提取以下格式的时间间隔(取自?xts):

    date['2007']  # all of 2007
    date['2007-03/']  # March 2007 to the end of the data set
    date['2007-03/2007']  # March 2007 to the end of 2007
    date['/'] # the whole data set
    date['/2007'] # the beginning of the data through 2007
    date['2007-01-03'] # just the 3rd of January 2007
    

    【讨论】:

    • 感谢 Striker 的快速回复。我遇到了 xts 包,并且已经快速查看了它。我只是觉得很难相信基础 R 本身没有其他选项,我必须为一个相当简单的任务安装一个额外的包。
    猜你喜欢
    • 2017-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-03
    • 1970-01-01
    • 1970-01-01
    • 2012-05-28
    • 2016-08-01
    相关资源
    最近更新 更多