Skip to content

Instantly share code, notes, and snippets.

@MrFlick
Created March 8, 2016 19:49
Show Gist options
  • Save MrFlick/d94be0ae8f1cb024b685 to your computer and use it in GitHub Desktop.
Save MrFlick/d94be0ae8f1cb024b685 to your computer and use it in GitHub Desktop.
to.minutes <-function(x) {
#regexpr("(\\d{1,2}:)?\\d{1,2}\\.\\d{1,2}", x, perl=T)
idx<-gregexpr(":", x, perl=T)
# try to turn hh:mm:ss interval to a count in minutes.
mapply(function(ss,ids) {
if(length(ids)==2) {
as.numeric(substr(ss,1,ids[1]-1)) * 60 +
as.numeric(substr(ss,ids[1]+1, ids[2]-2))+ as.numeric(substr(ss,ids[2]+1, nchar(ss)))/60
} else if (length(ids)==1) {
as.numeric(substr(ss,1,ids[1]-1)) + as.numeric(substr(ss,ids[1]+1, nchar(ss)))/60
} else {
NA
}
}, x, idx)
}
to.minutes(c("15:55", "1:30", "1:30:40.1"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment