Skip to content

Instantly share code, notes, and snippets.

@TonyLadson
Created August 18, 2016 04:20
Show Gist options
  • Save TonyLadson/2d9ae16247d8c1424167b14c68f471a2 to your computer and use it in GitHub Desktop.
Save TonyLadson/2d9ae16247d8c1424167b14c68f471a2 to your computer and use it in GitHub Desktop.
Convert from HH:MM:SS to fractions of a day.
# Convert from 'HH:MM:SS' to fractions of a day
# This also works with HH.HHHH
# and is vectorised
Time_convert <- function(x){
f <- function(x,y){
as.numeric(x)/60 + as.numeric(y)
}
split_time <- str_split(x, ':')
sapply(split_time, function(x){ as.numeric( Reduce(f, rev(x)) )/24})
}
@TonyLadson
Copy link
Author

The use of Reduce makes this efficient and flexible. The same code works for HH:MM:SS, HH:MM, HH, HH.HHHH

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment