问题描述
本质上,我只需要 R 中一列时间戳中的小时、分钟和秒,因为我想查看不同数据点在一天中的不同时间出现的频率,而日期与日期无关.
Essentially, I want only the hour, minute, and seconds from a column of timestamps I have in R, because I want to view how often different data points occur throughout different times of day and day and date is irrelevant.
然而,这是时间戳在数据集中的结构方式:2008-08-07T17:07:36Z
However, this is how the timestamps are structured in the dataset:2008-08-07T17:07:36Z
而且我不确定如何只从这个时间戳中获得那个时间.
And I'm unsure how to only get that time from this timestamp.
感谢您提供的任何帮助,如果我能提供更多信息,请告诉我!
Thank you for any help you can provide and please just let me know if I can provide more information!
推荐答案
我们可以使用 strptime
转换为 datetime 类,然后 format
提取小时:min:秒.
We can use strptime
to convert to a datetime class and then format
to extract the hour:min:sec.
dtime <- strptime(str1, "%Y-%m-%dT%H:%M:%SZ")
format(dtime, "%H:%M:%S")
#[1] "17:07:36"
如果 OP 想要将小时、分钟、秒作为单独的列
If the OP wants to have the hour, min, sec as separate columns
read.table(text=format(dtime, "%H:%M:%S"), sep=":", header=FALSE)
# V1 V2 V3
#1 17 7 36
另一种选择是使用 lubridate
library(lubridate)
format(ymd_hms(str1), "%H:%M:%S")
#[1] "17:07:36"
数据
str1 <- "2008-08-07T17:07:36Z"
这篇关于从时间戳中提取时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!