简体   繁体   English

在R中减去超过24小时的时间数据

[英]Substracting time data exceeding 24h in R

My data consists of time points, in hours, starting from the start point of the experiment. 我的数据包括从实验起点开始的时间点(以小时为单位)。 Experiments usually take over a week, so the the amount of hours easily exceeds 24. 实验通常需要一周以上的时间,因此小时数很容易超过24小时。

To be precise, data is in the following format: 162:43:33.281 hhh:mm:ss.msecs at the start of the experiment data points could consist of just 1-2 values for the hour insetad of the 3 mentioned here. 确切地说,数据采用以下格式:162:43:33.281 hhh:mm:ss.msecs在实验开始时,数据点可能只包含此处提到的3点的小时插入值的1-2个值。

When I try to substract 2 times points I get an error stating that the numerical expression has for exemple 162:43 elements, which obviously refers to the colon used in the time annotation. 当我尝试减去2点时,我得到一个错误,指出数值表达式具有示例162:43的元素,这显然是指时间注释中使用的冒号。

Any ideas on how to be able to treat time variables that consist of hour values over 24? 关于如何处理包含24小时以上小时值的时间变量的任何想法? I tried the strptime function, with %H as argument, but that limits me to 24 hours. 我尝试了以%H作为参数的strptime函数,但这将我限制为24小时。

Here is some example data: 以下是一些示例数据:

V1           V2 V3        V4                       V5             
75:45:32.487 NA 17       ####revFalsePoke is 112  TRUE
75:45:32.487 NA 17          ####totalwindow is 5  TRUE
75:46:32.713 NA  1          ####Criteria not met  TRUE
75:46:49.846 NA  6      ####revCorrectPoke is 37  TRUE
75:46:52.336 NA  9   ####revDeliberateLick is 34  TRUE
75:46:52.351 NA  9          ####totalwindow is 5  TRUE
75:46:52.598 NA  1          ####Criteria not met  TRUE
75:47:21.332 NA  6      ####revCorrectPoke is 38  TRUE
75:47:23.440 NA  9   ####revDeliberateLick is 35  TRUE
75:47:23.455 NA  9          ####totalwindow is 6  TRUE
75:47:23.657 NA  1      ####rev Criteria not met  TRUE
75:47:44.731 NA 17       ####revFalsePoke is 113  TRUE
75:47:44.731 NA 17          ####totalwindow is 6  TRUE

Unfortunately, you're going to have to roll your own converter function for this. 不幸的是,您将为此不得不使用自己的转换器功能。 I suggest converting the timestamps to difftime objects (which represent time duration, rather than location). 我建议将时间戳转换为difftime对象(代表持续时间,而不是位置)。 You can then add them to some starting datetime to arrive at a final datetime for each timestamp. 然后,您可以将它们添加到某个开始日期时间,以到达每个时间戳的最终日期时间。 Here's one approach: 这是一种方法:

f <- function(start, timestep) {
    result <- mapply(function(part, units) as.difftime(as.numeric(part), units=units), 
                     unlist(strsplit(timestep, ':')), 
                     c('hours', 'mins', 'secs'),
                     SIMPLIFY=FALSE)
    start + do.call(sum, result)
}

start <- as.POSIXct('2013-1-1')
timesteps <- c('162:43:33.281', '172:34:28.33')
lapply(timesteps, f, start=start)
# [[1]]
# [1] "2013-01-07 18:43:33.280 EST"
# 
# [[2]]
# [1] "2013-01-08 04:34:28.32 EST"

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM