从时间戳中提取小时、分钟和秒
Extracting hour, minute and second from timestamp
如何从 R 中的数据帧的时间戳中提取小时、分钟和秒?我的代码是
lubridate::ymd_hms(d1$timestamp, tz = "UTC")
d2= lubridate::ymd_hms(d1$timestamp, tz = "UTC")
d3= lubridate::hms(d2)
试试 lubridate 包。
> library(libridate)
> k <- ymd_hms('2018-11-08T07:41:55.921Z')
> minute(k)
[1] 41
> second(k)
[1] 55.921
先转换为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
如何从 R 中的数据帧的时间戳中提取小时、分钟和秒?我的代码是
lubridate::ymd_hms(d1$timestamp, tz = "UTC")
d2= lubridate::ymd_hms(d1$timestamp, tz = "UTC")
d3= lubridate::hms(d2)
试试 lubridate 包。
> library(libridate)
> k <- ymd_hms('2018-11-08T07:41:55.921Z')
> minute(k)
[1] 41
> second(k)
[1] 55.921
先转换为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