【发布时间】:2017-06-24 18:12:52
【问题描述】:
我创建了一个从 00:00:00 hrs 到 23:00:00 hrs 的时间序列。添加任何小时数都很容易,直到总和小于或等于 23:00:00 小时。总和超过 23:00:00 后,它开始以天数显示时间,这非常直观。但我希望在时钟时间内输出,无论我是做减法还是加法
假设我想像下面那样做加法
library(chron)
times("23:00:00")+ times("01:00:00")
Time in days:
[1] 1
我想要的输出低于一。而不是得到我想要的日子
00:00:00
我也试过减法
times("00:00:00")- times("01:00:00")
[1] -0.04166667
期望的输出
"23:00:00"
我也用 POSIXct 进行了尝试,但它在各种情况下都会出现各种错误
as.POSIXct("00:00:00", format = "%H:%M:%S", tz = "UTC")
[1] "2017-02-07 UTC" #Not printing time. Only dates
使用 POSIXct 减法
as.POSIXct("00:00:00", format = "%H:%M:%S", tz = "UTC") -as.POSIXct("01:00:00", format = "%H:%M:%S", tz = "UTC")
Time difference of -1 hours
Warning message:
In 1:0:0 : numerical expression has 2 elements: only the first used
使用 POSIXct 加法
as.POSIXct("23:00:00", format = "%H:%M:%S", tz = "UTC") + as.POSIXct("01:00:00", format = "%H:%M:%S", tz = "UTC")
Error in `+.POSIXt`(as.POSIXct("23:00:00", format = "%H:%M:%S", tz = "UTC"), :
binary '+' is not defined for "POSIXt" objects
请帮我解决这个问题。另外,请帮助我在上面显示的 POSIXct 中打印时间和日期如果有什么对您没有意义,请告诉我,而不是对我的问题投反对票。
【问题讨论】:
-
您是否尝试过定义
as.POSIXct( format = "%Y-%m-%d %H:%M:%S")。将年、月、日保留为 0,但对其进行初始化。它将帮助您保持跟踪。在 R 中还有一个difftime函数,它减去两个日期时间之间的时间。我会使用它而不是减去它。 -
times定义在哪里? -
@nrussell 它在 chron 库中
-
@Dinesh.hmn 我的目的不是测量时差。
-
标签: r datetime time time-series