简体   繁体   English

R中的时间数据以HMS格式记录持续时间(但H可以> 24)

[英]Time data in R for logged durations in H M S format (but H can be > 24)

I have a data set like 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 to get it into plottable format like so: 我试图像这样将其转换成绘图格式:

library(reshape2)
library(scales)

# melt the data and convert the time strings to 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 for the final durations of Dur9 presumably due to the fact that POSOXct is expecting actual HMS data in the sense of 24h time. 我得到NA S代表的最终持续时间Dur9大概是由于POSOXct在24小时的时间观念预期实际HMS数据的事实。

What's the recommended way to deal with logged data like this that isn't rolling over into days once H > 24 ? 建议使用什么方法来处理这样的记录数据,一旦H > 24 ,这些记录数据就不会累积到几天?

Do I need to manually check it for such instances and create a new string representing the days (which would then seem to require that I create an arbitrary start day and increment the day if H > 24 )? 我是否需要手动检查此类实例并创建一个表示日期的新字符串(这似乎要求我创建一个任意的开始日期,如果H > 24则增加该天)? Or is there a package better suited for strict time data vs. assuming all time data is logged according to an actual time stamp? 还是有一个更适合于严格时间数据的软件包,而不是假设所有时间数据都是根据实际时间戳记录的?

Many thanks! 非常感谢!

You can use colsplit from the plyr package to create columns for hours, minutes and seconds then using create a difftime object that can be added to a date 您可以使用colsplit包中的plyr创建小时,分钟和秒的列,然后使用创建可以添加到日期的difftime对象

library(plyr)

# note gsub('s','',mdd[['value']]) removes trailing s from each value
# we then split on `[hm]` (ie. h or m)` -- this returns a data.frame with
# 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 to do the arithmetic yourself, using Map and Reduce 您不需要使用MapReduce自己进行算术运算

 Reduce('+',Map(as.difftime, times, units = c('hours','mins','secs')))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 以 R 格式计算 %H:%M:%S 格式的时间差 - Calculate time difference in %H:%M:%S format in R 在R中将变量转换成%H:%M时间格式 - Transform variable into a %H:%M time format in R R POSIX%H:%M:%S平均时间 - R POSIX %H:%M:%S Time Average R 中有没有办法将我的 DateTime 列转换为日期和时间格式(“%m/%d/%Y”和“%h/%m/%s”) - Is there a way in R to convert my DateTime column into Date and Time with the format ("%m/%d/%Y" and "%h/%m/%s") 如何在r数据帧中将日期时间格式“%Y-%m-%d%H:%M:%S”转换为“%Y-%m-%d%H:%M:%S.sss”? 第二部分 - how to convert date time format “%Y-%m-%d %H:%M:%S” to “%Y-%m-%d %H:%M:%S.sss” in r dataframe? second with fraction 在R中减去超过24小时的时间数据 - Substracting time data exceeding 24h in R 在R中选择带有窗口的时间段; 格式为“ dd / mm / yyyy h:m:s”(Windows 7) - Selecting a time period with window in R; format “dd/mm/yyyy h:m:s” (Windows 7) R - 将“%d/%m/%Y %H:%M”格式的每小时数据转换为“%d/%m/%Y %H:%M:%S”且 as.Date 和 as.POSIXct not在职的 - R - Converting Hourly Data in “%d/%m/%Y %H:%M” format to “%d/%m/%Y %H:%M:%S” with as.Date an as.POSIXct not Working 在R中将%H:%M:%S添加为as.POSIXct格式 - Add %H:%M:%S into as.POSIXct format in R 处理由天,小时,分钟和秒定义的持续时间,例如R中的“1d 3h 2m 28s” - Dealing with durations defined by days, hours, minutes and seconds such as “1d 3h 2m 28s” in R
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM