R:如何根据不同的开始时间将经过的秒数转换为一天中的时间

R: How to convert elapsed seconds to time of day based on different start times

我想将 "TOTALSEC" 中的秒数转换为一天中的 24 小时制时间(即 14:32:40)。不过需要以"Time"中的开始时间为准。因此,例如,我在 Time 下有“12:00:00”作为开始时间,在 TOTALSEC 下有“3630”。我想获得转换后的时间以读取 13:00:30。对于上下文,时间是开始时间一个录音机,TOTALSEC 是自开始时间以来音频片段出现的#秒数。因此在上面的示例中,我在 12:00:00 开始录制,音频片段在开始录制后 3630 秒出现。

> head(stacksubset)
# A tibble: 6 x 2
TOTALSEC    Time     Date
    <dbl>          <chr>    <chr>
1 10613.67 15:53:50 2017-05-30
2 56404.35 17:29:44 2017-05-29
3 20480.54 16:16:12 2017-06-13
4 60613.47 15:53:50 2017-05-30
5 80034.30 16:16:12 2017-06-02
6 50710.37 16:16:12 2017-05-27

我在想它可能必须是某种循环函数(上面 6 行之后还有 2000 行)。

数据

data <- data.frame(TOTALSEC = c(10613.67, 56404.35, 20480.54, 60613.47, 80034.30, 50710.37),
                   Time = c("15:53:50", "17:29:44", "16:16:12", "15:53:50", "16:16:12", "16:16:12"))

代码

因为日期实际上在这里很重要,所以每个时间都被粘贴到一个日期(今天的日期),然后转换为您系统时区的日期时间。 然后相加。

library(lubridate)
library(dplyr)

data %>% mutate(Time = as_datetime(paste(Date, Time), Sys.timezone(location = TRUE)),
                end_time = TOTALSEC + Time)

结果

  TOTALSEC                Time            end_time
1 10613.67 2017-11-23 15:53:50 2017-11-23 18:50:43
2 56404.35 2017-11-23 17:29:44 2017-11-24 09:09:48
3 20480.54 2017-11-23 16:16:12 2017-11-23 21:57:32
4 60613.47 2017-11-23 15:53:50 2017-11-24 08:44:03
5 80034.30 2017-11-23 16:16:12 2017-11-24 14:30:06
6 50710.37 2017-11-23 16:16:12 2017-11-24 06:21:22