【问题标题】:Is there a way to aggregate by time on xts regardless the date无论日期如何,有没有办法在 xts 上按时间聚合
【发布时间】:2019-12-28 00:16:27
【问题描述】:

我正在尝试按小时/时间拆分,而日期不会影响结果,我正在使用按日期和时间索引的 xts 对象

当我使用按小时拆分时,我得到了结果,但在日期内,我希望忽略日期,只按时间得到,试图去掉日期并返回 posixct,但没有帮助,我也尝试将 period.apply 与端点一起使用,但结果相同。

lapply(split(temp[,"GROSS"] , f = "hour"), FUN = cumsum)
[[1]]
                    GROSS
2018-10-12 09:46:38 "11" 

[[2]]
                    GROSS 
2018-10-12 10:04:08 "-4"  
2018-10-12 10:23:58 "5.2" 
2018-10-12 10:24:08 "-1.1"

[[3]]
                    GROSS 
2018-10-15 09:35:46 "20.7"

[[4]]
                    GROSS 
2018-10-17 09:30:56 "-7.2"

[[5]]
                    GROSS 
2018-10-17 10:44:48 "13.5"

我希望得到没有日期的按小时累积的结果。

【问题讨论】:

  • 请通过dput(head(temp[,"GROSS"]))提供您的数据示例
  • > dput(head(temp[,"GROSS"])) 结构(c(“11.00”,“-4.00”,“9.20”,“-6.30”,“20.70”,“- 7.20"), class= c("xts", "zoo"), .indexCLASS = c("POSIXlt", "POSIXt"), tclass= c("POSIXlt", "POSIXt"), .indexTZ = "UTC" , tzone = "UTC", index = 结构(c(1539337598, 1539338648, 1539339838, 1539339848, 1539596146, 1539768656), tzone = "UTC", tclass= c("POSIXlt", "cPOSIXt")), .Dim (6L, 1L), .Dimnames = list(NULL, "GROSS")) @shs

标签: r xts


【解决方案1】:

在处理日期、时间或日期时间变量时,lubridate 包非常有用。

library(xts)
library(lubridate)

df_ts <- structure(
  c(" 11.00", " -4.00", " 9.20", " -6.30", " 20.70", " -7.20"), 
  class = c("xts", "zoo"), .indexCLASS = c("POSIXlt", "POSIXt"), 
  tclass = c("POSIXlt", "POSIXt"), 
  .indexTZ = "UTC", 
  tzone = "UTC", 
  index = structure(c(1539337598, 1539338648, 1539339838, 1539339848, 1539596146, 1539768656), 
                    tzone = "UTC", 
                    tclass = c("POSIXlt", "POSIXt")), 
  .Dim = c(6L, 1L), 
  .Dimnames = list(NULL, "GROSS"))

lapply(split(df_ts, 
             f = hour(as_datetime(attr(df_ts, "index")))), 
       FUN = cumsum)

【讨论】:

    猜你喜欢
    • 2014-04-12
    • 2017-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-13
    • 2021-11-22
    • 1970-01-01
    • 2014-08-23
    相关资源
    最近更新 更多