我有一个HH:MM:SS格式的两列“开始”和“结束”的数据框。
我想使用difftime函数计算开始和结束之间的持续时间
它总是返回此错误: as.POSIXct.numeric(time1) 中的错误:必须提供'origin'
我读了很多帖子,但似乎没有一个对我有用。
加载包
library(dplyr)
library(tidyverse)
library(lubridate)
我删除了几个小时来处理分钟和秒
get_time <- function(x){str_sub(x, start = -5) %>% ms()}
df <- df %>% mutate(start = get_time(start)) %>%
mutate(end = get_time(end))
对象类别
class(df$start)
gives:
[1] "Period"
attr(,"package")
[1] "lubridate"
start end
26M 22S 26M 23S
26M 25S 26M 37S
29M 47S 30M 13S
我使用difftime函数计算了持续时间
df$duration <- with(df, difftime(end, start, units="secs"))
gives error:
Error in as.POSIXct.numeric(time1) : 'origin' must be supplied
我使用减法运算符,它工作正常,除了第 3 行分钟不同时,它给出了错误的答案。
start end duration
26M 22S 26M 23S 1S
26M 25S 26M 37S 12S
29M 47S 30M 13S 1M -34S
修正案
接受的响应工作得很好,除了它返回一个错误: mtx1[3, ] 中的错误:每当应用于我在同一数据框中的第二个两列“start2”和“end2”时,维度数不正确。
来自我的 df 的样本
df <- structure(list(item = c("manatee", "manatee", "pile", "pile"), prestart = new("Period", .Data = c(22,
25, 41, 49), year = c(0, 0, 0, 0), month = c(0,
0, 0, 0), day = c(0, 0, 0, 0), hour = c(0, 0, 0,
0), minute = c(26, 26, 26, 26)), preend = new("Period",
.Data = c(23, 37, 48, 50), year = c(0, 0, 0, 0), month = c(0, 0, 0, 0), day = c(0, 0, 0, 0
), hour = c(0, 0, 0, 0), minute = c(26, 26, 26, 26)), poststart = new("Period", .Data = c(23, 41, 50,
54), year = c(0, 0, 0, 0), month = c(0, 0, 0, 0), day = c(0, 0, 0, 0), hour = c(0, 0, 0, 0),
minute = c(26, 26, 26, 26)), postend = new("Period",
.Data = c(37, 48, 52, 22), year = c(0, 0, 0, 0), month = c(0, 0, 0, 0), day = c(0, 0, 0, 0
), hour = c(0, 0, 0, 0), minute = c(26, 26, 26, 27))), row.names = c(NA, -6L), class = c("tbl_df", "tbl",
"data.frame"))
仅以分钟和秒为单位组织数据(删除小时)
get_time <- function(x){str_sub(x, start = -5) %>% ms()}
df <- df %>% mutate(prestart = get_time(prestart)) %>%
mutate(preend = get_time(preend)) %>%
mutate(poststart = get_time(poststart)) %>%
mutate(postend = get_time(postend))