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

2015-05-05 Thread gavinr
Thanks guys. The first solution with the gsub / lapply works perfectly. The solution using substrings would work if the times were in a consistent format, but without the leading zeros and with some parts of the string absent completely it would need some extra logic to apply. I need something to a

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

2015-05-05 Thread Franklin Bretschneider
Hello gavinr, > 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

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

2015-05-04 Thread John Laing
Regular expressions are the tool for this problem. This pattern matches your input data: t <- c("10H20M33S", "1H1M", "1M", "21M9S", "2H55S") patt <- "^(([0-9]+)H)?(([0-9]+)M)?(([0-9]+)S)?$" all(grepl(patt, t)) # TRUE We can use the pattern to extract hour/minute/second components hms <- lapply(c