datetime - Time data in R for logged durations in H M S format (but H can be > 24) -
i have data set this:
> dput(data) structure(list(run = c("dur 2", "dur 3", "dur 4", "dur 5", "dur 7", "dur 8", "dur 9"), reference = c("00h 00m 32s", "00h 00m 31s", "00h 05m 46s", "00h 03m 51s", "00h 06m 49s", "00h 06m 47s", "00h 08m 56s" ), test30 = c("00h 00m 44s", "00h 00m 41s", "00h 21m 54s", "00h 13m 37s", "00h 28m 48s", "00h 22m 54s", "10h 02m 12s"), test31 = c("00h 00m 39s", "00h 00m 45s", "00h 40m 10s", "00h 23m 07s", "00h 35m 23s", "00h 47m 42s", "25h 37m 05s"), test32 = c("00h 01m 05s", "00h 01m 13s", "00h 55m 02s", "00h 28m 54s", "01h 03m 17s", "01h 02m 08s", "39h 04m 39s")), .names = c("run", "reference", "test30", "test31", "test32"), class = "data.frame", row.names = c(na, -7l))
i tried plottable format so:
library(reshape2) library(scales) # melt data , convert time strings posixct format data_melted <- melt(data, id.var = "run") data_melted$value <- as.posixct(data_melted$value, format = "%hh %mm %ss")
i'm getting na
s final durations of dur9
presumably due fact posoxct expecting actual hms data in sense of 24h time.
what's recommended way deal logged data isn't rolling on days once h > 24
?
do need manually check such instances , create new string representing days (which seem require create arbitrary start day , increment day if h > 24
)? or there package better suited strict time data vs. assuming time data logged according actual time stamp?
many thanks!
you can use colsplit
plyr
package create columns hours, minutes , seconds using create difftime
object can added date
library(plyr) # note gsub('s','',mdd[['value']]) removes trailing s each value # split on `[hm]` (ie. h or m)` -- returns data.frame # 3 integer columns times <- colsplit(gsub('s','',mdd[['value']]), '[hm]', names = c('h','m','s')) seconds <- as.difftime(with(times, h*60*60 + m *60 + s), format = '%x', units = 'secs') seconds time differences in secs [1] 32 31 346 231 409 407 536 44 41 1314 817 1728 1374 36132 39 45 [17] 2410 1387 2123 2862 92225 65 73 3302 1734 3797 3728 140679
you don't need arithmetic yourself, using map
, reduce
reduce('+',map(as.difftime, times, units = c('hours','mins','secs')))
Comments
Post a Comment