Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
398 views
in Technique[技术] by (71.8m points)

r - Extract time from timestamp?

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.

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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"

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

Another option is using lubridate

library(lubridate)
format(ymd_hms(str1), "%H:%M:%S")
#[1] "17:07:36"

data

str1 <- "2008-08-07T17:07:36Z"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...