【问题标题】:Excluding hours and days in xts不包括 xts 中的小时和天数
【发布时间】:2015-05-09 10:45:27
【问题描述】:

我有一个 xts 对象,其中包含从 1.1.2007 到 5.6.2014 的 NA 数据,间隔为分钟。我需要在周五 17:00 到周日 17:00 之间排除。 我知道像 ['T17:00:00/T17:00:00'] 这样子集的技巧,但是您如何处理星期几条件以将范围排除在其中? 所以,在这,

dt.seq <- seq(c(ISOdate(2007,01,01)),c(ISOdate(2014,05,06)),by="min")
dt.NA <- xts(rep(NA,length(dt.seq)),dt.seq)

我不想周五到周日的 17:00 到 17:00 之间

谢谢

【问题讨论】:

    标签: r subset xts weekday


    【解决方案1】:

    这是我首先想到的;可能有更好的方法。

    require(xts)
    dt.seq <- seq(c(ISOdate(2007,01,01)),c(ISOdate(2014,05,06)),by="min")
    dt.NA <- xts(rep(NA,length(dt.seq)),dt.seq)
    
    wday <- .indexwday(dt.NA)       # weekday for each observation
    hour <- .indexhour(dt.NA)       # hour for each observation
    week.subset <-
      !((wday == 5 & hour >= 17) |  # Friday, after 17:00
        (wday == 6) |               # Saturday, all day
        (wday == 0 & hour < 17))    # Sunday, before 17:00
    
    # Now subset your xts object
    x <- dt.NA[week.subset,]
    # Verify it only has the observations you want
    table(.indexhour(x), .indexwday(x))
    

    【讨论】:

      猜你喜欢
      • 2015-09-02
      • 1970-01-01
      • 2011-11-06
      • 2019-10-22
      • 1970-01-01
      • 2020-05-11
      • 1970-01-01
      • 2016-12-29
      • 2020-12-29
      相关资源
      最近更新 更多