舍入毫秒
Rounding miliseconds
我试图将我的时间列从显示毫秒四舍五入到只显示 hms
但没有成功
我试图让它与 floor_date()
和 round_date()
一起工作,但没有任何运气。
有人知道如何将我的毫秒舍入为整秒吗?
非常感谢所有帮助!
df <- structure(list(date = c("2020-12-15", "2020-12-15", "2020-12-15"
), time = c("09:19:26.599", "09:19:30.391", "09:19:31.142")), row.names = c(NA,
-3L), class = "data.frame")
你可以用 as.POSIXct
在 base R 中做到这一点
transform(df, datetime = round(as.POSIXct(paste(date, time), tz = 'UTC')))
# date time datetime
#1 2020-12-15 09:19:26.599 2020-12-15 09:19:27
#2 2020-12-15 09:19:30.391 2020-12-15 09:19:30
#3 2020-12-15 09:19:31.142 2020-12-15 09:19:31
使用 dplyr
和 lubridate
:
library(dplyr)
df %>% mutate(datetime = round(lubridate::ymd_hms(paste(date, time))))
我试图将我的时间列从显示毫秒四舍五入到只显示 hms
但没有成功
我试图让它与 floor_date()
和 round_date()
一起工作,但没有任何运气。
有人知道如何将我的毫秒舍入为整秒吗?
非常感谢所有帮助!
df <- structure(list(date = c("2020-12-15", "2020-12-15", "2020-12-15"
), time = c("09:19:26.599", "09:19:30.391", "09:19:31.142")), row.names = c(NA,
-3L), class = "data.frame")
你可以用 as.POSIXct
transform(df, datetime = round(as.POSIXct(paste(date, time), tz = 'UTC')))
# date time datetime
#1 2020-12-15 09:19:26.599 2020-12-15 09:19:27
#2 2020-12-15 09:19:30.391 2020-12-15 09:19:30
#3 2020-12-15 09:19:31.142 2020-12-15 09:19:31
使用 dplyr
和 lubridate
:
library(dplyr)
df %>% mutate(datetime = round(lubridate::ymd_hms(paste(date, time))))