我试图按小时/时间进行划分,而日期不会影响结果,我正在使用按日期和时间索引的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"
我希望按小时数而不是日期来累积结果。
答案 0 :(得分:0)
在处理日期,时间或日期时间变量时,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)