How can I extract the hour, minute and second from this timestamp from a data frame in R? My code is
lubridate::ymd_hms(d1$timestamp, tz = "UTC")
d2= lubridate::ymd_hms(d1$timestamp, tz = "UTC")
d3= lubridate::hms(d2)
How can I extract the hour, minute and second from this timestamp from a data frame in R? My code is
lubridate::ymd_hms(d1$timestamp, tz = "UTC")
d2= lubridate::ymd_hms(d1$timestamp, tz = "UTC")
d3= lubridate::hms(d2)
Try the lubridate package.
> library(libridate)
> k <- ymd_hms('2018-11-08T07:41:55.921Z')
> minute(k)
[1] 41
> second(k)
[1] 55.921
First convert to POSIXct
x <- "2018-11-08T07:41:55.921Z"
x.ct <- as.POSIXct(x, format="%Y-%m-%dT%H:%M:%OS")
Then extract
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"
Or as 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