Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

r - How to convert time to decimal

I have a large data set containing time in hh:mm:ss format and I would like to convert these to a decimal format while ignoring the hours (hh). I have used strptime but this does not seem to convert it.

To be more precise I would like to change 00:01:38 into 1,6333 and 01:18:30 into 78,5.

How do I do this in R?

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

You can use stringsplit and sapply

dat<-c('00:01:38','01:18:30')
sapply(strsplit(dat,":"),
       function(x) {
         x <- as.numeric(x)
         x[1]*60+x[2]+x[3]/60
       }
)

Result:

[1]  1.633333 78.500000

Credits go to @Joris Meys

Just extended his example: How to convert time (mm:ss) to decimal form in R


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...