从时间戳中提取小时,分钟和秒

时间:2019-01-03 19:46:23

标签: r datetime datetime-format

如何从R中的数据帧中从此时间戳中提取小时,分钟和秒?我的代码是

lubridate::ymd_hms(d1$timestamp, tz = "UTC")
d2= lubridate::ymd_hms(d1$timestamp, tz = "UTC")
d3= lubridate::hms(d2)

2 个答案:

答案 0 :(得分:0)

尝试润滑包装。

> library(libridate)
> k <- ymd_hms('2018-11-08T07:41:55.921Z')
> minute(k)
[1] 41
> second(k)
[1] 55.921

答案 1 :(得分:0)

首先转换为POSIXct

x <- "2018-11-08T07:41:55.921Z"
x.ct <- as.POSIXct(x, format="%Y-%m-%dT%H:%M:%OS")

然后提取

format(x.ct, "%H") # hours
# [1] "07"

format(x.ct, "%M") # minutes
# [1] "41"

format(x.ct, "%S") # seconds (truncated integer)
# [1] "55"

format(x.ct, "%OS3") # seconds (three decimal places)
# [1] "55.921"

或作为POSIXlt

x.lt <- as.POSIXlt(x, format="%Y-%m-%dT%H:%M:%OS")

x.lt$hour
# 7

x.lt$min
# 41

x.lt$sec
# 55.921
相关问题