我有一个这样的数据框:
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_start
和time_end
的类型为character
(class(df$time_start)
)。
现在,我想创建一个新列duration
,其中包含time_start
和time_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_start
和time_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
答案 0 :(得分:0)
根据OP的要求,将评论转换为答案:
library(lubridate)
df$duration <- difftime(ymd_hms(df$end), ymd_hms(df$start))