如何提取时间戳并估计它们之间的差异

How to extract timestamps and estimate the difference between pairs of them

我有一个这样的数据框:

df = data.frame(c("2012-12-30 08:01:01","2012-12-30 09:05:02"),
                c("2012-12-30 09:01:00","2012-12-30 10:15:00"))
colnames(df) = c("start","end")

然后我提取时间戳如下:

df$time_start = substr(df$start,12,19)
df$time_end = substr(df$end,12,19)

time_starttime_end的类型是characterclass(df$time_start))。

现在我想创建一个新列 duration,它将包含 time_starttime_end 之间的差异:

library("lubridate")

df$duration <- seconds_to_period(as.numeric(difftime(df$time_start, dfs$time_end, units = "secs"))

我收到以下错误:

Error in as.POSIXlt.character(x, tz, ...) : 
  character string is not in a standard unambiguous format

我试图将 time_starttime_end 解析为 POSIXct,但我又得到了日期:

df$time_start_parsed = as.POSIXct(df$time_start,format="%H:%M:%S")

df

                start                 end time_start   time_start_parsed
1 2012-12-30 08:01:01 2012-12-30 09:01:00   09:01:00 2016-10-19 09:01:00
2 2012-12-30 09:05:02 2012-12-30 10:15:00   10:15:00 2016-10-19 10:15:00

根据 OP 的要求,将评论转换为答案:

library(lubridate)
df$duration <- difftime(ymd_hms(df$end), ymd_hms(df$start))