【问题标题】:R help in subsetting daily range with POSIXctR 帮助使用 POSIXct 对每日范围进行子集化
【发布时间】:2014-11-15 08:51:52
【问题描述】:

我正在尝试对 POSIXct 时间序列的每一天的间隔进行子集化。

假设我有一个 3 天的样本集,每 15 分钟采样一次。

sample <- seq(as.POSIXct("2000-01-01 00:00:00"),as.POSIXct("2000-01-03 24:00:00"),by=15*60)

 [1] "2000-01-01 00:00:00 PST" "2000-01-01 00:15:00 PST" "2000-01-01 00:30:00 PST" "2000-01-01 00:45:00 PST" "2000-01-01 01:00:00 PST" "2000-01-01 01:15:00 PST" "2000-01-01 01:30:00 PST" "2000-01-01 01:45:00 PST"
 [9] "2000-01-01 02:00:00 PST" "2000-01-01 02:15:00 PST" "2000-01-01 02:30:00 PST" "2000-01-01 02:45:00 PST" "2000-01-01 03:00:00 PST" "2000-01-01 03:15:00 PST" "2000-01-01 03:30:00 PST" "2000-01-01 03:45:00 PST"
[17] "2000-01-01 04:00:00 PST" "2000-01-01 04:15:00 PST" "2000-01-01 04:30:00 PST" "2000-01-01 04:45:00 PST"

使用 lubridate 包,我可以很容易地按小时间隔进行子集化。

sample_subset <- sample[hour(sample) >= 9 & hour(sample) =< 12]

 [1] "2000-01-01 10:00:00 PST" "2000-01-01 10:15:00 PST" "2000-01-01 10:30:00 PST" "2000-01-01 10:45:00 PST" "2000-01-01 11:00:00 PST" "2000-01-01 11:15:00 PST" "2000-01-01 11:30:00 PST" "2000-01-01 11:45:00 PST"
"2000-01-02 10:00:00 PST" "2000-01-02 10:15:00 PST" "2000-01-02 10:30:00 PST" "2000-01-02 10:45:00 PST" "2000-01-02 11:00:00 PST" "2000-01-02 11:15:00 PST" "2000-01-02 11:30:00 PST" "2000-01-02 11:45:00 PST"

问题在于如何对每天固定的每小时/分钟间隔进行子集化。我想从 9:30 开始子集 到每天中午 12:00。如果我只是添加一个过滤器,例如分钟(样本)> 30,它将过滤掉范围内每个小时的分钟数。

我看了几个相关的帖子;但他们只显示每小时过滤。似乎应该有一个我不理解的相当简单的子集条件。像 sample[minute(sample)[hour(sample)==9] >30] 这样的东西,但它不起作用。还有其他简单的想法吗?

*编辑

基于 ilister 的想法,我只是用 lubridate 扩展了布尔索引。 我不知何故错过了 ORing。

cond1 <-  hour(sample) >= 9 & minute(sample) > 30

cond2 <-  hour(sample) < 12

cond3 <-  hour(sample) > 9

       sample[(cond1 | cond3) & cond2] 

       "2000-01-01 09:45:00 PST" "2000-01-01 10:00:00 PST" "2000-01-01 10:15:00 PST" "2000-01-01 10:30:00 PST" "2000-01-01 10:45:00 PST"
   "2000-01-01 11:00:00 PST" "2000-01-01 11:15:00 PST" "2000-01-01
   11:30:00 PST"

【问题讨论】:

    标签: r posixct


    【解决方案1】:

    尝试使用 indexClass {xts} 中的联合条件进行子集化:

    require(xts)
    sample <- seq(as.POSIXct("2000-01-01 00:00:00"),
                  as.POSIXct("2000-01-03 24:00:00"),by=15*60)
    xsample <- xts(1:289, order.by=sample)
    xsample[.indexhour(xsample)==9 & .indexmin(xsample) %in% 15:59]
    

    将返回集合中 9:15 到

    然后将该索引与标准 .indexhour 连接起来以返回 10:00 到 12:00 的集合。

    xsample[c(which(.indexhour(xsample)==9 & .indexmin(xsample) %in% 15:59),
              which(.indexhour(xsample) %in% 10:11))]
    

    或者,如果您对布尔运算符感到满意,那就更优雅:

    xsample[.indexhour(xsample)==9 & .indexmin(xsample) %in% 15:59 |
            .indexhour(xsample) %in% 10:11]
    

    【讨论】:

    • 看到问题中的预期结果,为什么不xsample["T09:30:00/T11:59:59"]
    【解决方案2】:

    在基础 R 中,POSIXlt 可能有用。试试这个:

        sampleLT<-as.POSIXlt(sample)
        secFromMidnigth<-sampleLT$hour*3600+sampleLT$min*60+sampleLT$sec
        sample[secFromMidnigth>9*3600+30*60 & secFromMidnigth<3600*12]
    

    您可以提取从给定日期时间的午夜开始的秒数,看看它是否大于 9*3600+30*60(09:30)和小于 3600*12(12:00)。

    我不太了解lubridate,但正如我从您的 OP 中看到的,您也可以这样做:

        sample[hour(sample)*60+minute(sample)>9*60+30 & hour(sample)<12]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-26
      相关资源
      最近更新 更多