Pra*_*til 6 statistics datetime r
假设我通过了"2015-01-01 01:50:50"
,那么它应该返回"2015-01-01 01:00:00"
并且"2015-01-01 02:00:00"
.如何在R中计算这些值?
假设你的时间是变量"X",你可以使用round
或trunc
.
尝试:
round(X, "hour")
trunc(X, "hour")
Run Code Online (Sandbox Code Playgroud)
这仍然需要一些工作来确定值是否实际上是向上舍入或向下舍入(for round
).所以,如果您不想考虑这一点,可以考虑使用"lubridate"包:
X <- structure(c(1430050590.96162, 1430052390.96162), class = c("POSIXct", "POSIXt"))
X
# [1] "2015-04-26 17:46:30 IST" "2015-04-26 18:16:30 IST"
library(lubridate)
ceiling_date(X, "hour")
# [1] "2015-04-26 18:00:00 IST" "2015-04-26 19:00:00 IST"
floor_date(X, "hour")
# [1] "2015-04-26 17:00:00 IST" "2015-04-26 18:00:00 IST"
Run Code Online (Sandbox Code Playgroud)
我会使用以下包装器使用基本R(您可以使用函数中的tz
参数指定您的时区strptime
)
Myfunc <- function(x){x <- strptime(x, format = "%F %H") ; c(x, x + 3600L)}
Myfunc("2015-01-01 01:50:50")
## [1] "2015-01-01 01:00:00 IST" "2015-01-01 02:00:00 IST"
Run Code Online (Sandbox Code Playgroud)