[R] A problem with string handling to make a time duration

gavinr g.rudge at bham.ac.uk
Mon May 4 21:59:08 CEST 2015


I have a character string that represents a time duration. It has an hours
minutes seconds structure(ish) but with letters denoting units (H,M or S) no
leading zeros and no placeholder at all where one or other of the units are
not required.

It looks like this:

t<-c("10H20M33S","1H1M","1M","21M9S","2H55S" ))
df<-data.frame(t)
df

#ideally should look like:
t2<-c("10:20:33","01:00:01","00:01:00","00:21:09","02:00:55") 
df2<-data.frame(t2)
df2

I need to get it into hours minutes and seconds either in time format or as
a string with leading zeros and all three time units represented in each
one, as in df2.  The data, part of a very large dataset, are for onward use
and processing in a GIS application.  I’ve messed about with string handling
statements in SQL to no avail, but wondered if R would be a better bet? 
I’ve had a look at some of the commands in stringr, but am unsure how to
operationalise a solution using this package.  Any advice is welcome.




--
View this message in context: http://r.789695.n4.nabble.com/A-problem-with-string-handling-to-make-a-time-duration-tp4706795.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list