你怎么把时间花在R的下一个小时

问题描述 投票:1回答:2

我有数据价值

dput(a)
"1/3/2019 15:59"

我需要将时间缩短到下一个小时。我需要这个日期是"1/3/2019 16:00"

我怎样才能做到这一点?

r
2个回答
8
投票

我们可以使用lubridate dmy_hm转换为datetime对象,然后使用ceiling_date将其转换为下一个小时。

library(lubridate)
ceiling_date(dmy_hm("1/3/2019 15:59"), "hour")
#[1] "2019-03-01 16:00:00 UTC"

1
投票

使用round.POSIXt。没有使用包裹。

x <- as.POSIXct("1/3/2019 15:59", format = "%m/%d/%Y %H:%M")
round(x + 3600/2 - !(as.numeric(x) %% 3600), "hours")
## [1] "2019-01-03 16:00:00 EST"
© www.soinside.com 2019 - 2024. All rights reserved.