下面是使用data.table
的可能实现。在这里,我们首先转换为POSIXct
类,然后计算每个名称的平均分钟数。如果你喜欢,你可以添加round
。
library(data.table)
setDT(df)[, `:=`(V1 = as.POSIXct(V1), V3 = as.POSIXct(V3))]
df[, mean(difftime(V1, V3, units = "mins")), by = V2]
# V2 V1
# 1: Nicole 17.98889 mins
# 2: Albert 64.73333 mins
# 3: Marlen 161.55000 mins
数据
df <- structure(list(V1 = structure(c(1L, 2L, 3L, 3L, 4L), .Label = c("2014-01-05 23:05:42",
"2014-01-06 13:02:58", "2014-01-08 03:04:49", "2014-01-08 08:26:41"
), class = "factor"), V2 = structure(c(3L, 1L, 3L, 3L, 2L), .Label = c("Albert",
"Marlen", "Nicole"), class = "factor"), V3 = structure(c(1L,
2L, 3L, 3L, 4L), .Label = c("2014-01-05 22:41:26", "2014-01-06 11:58:14",
"2014-01-08 02:49:58", "2014-01-08 05:45:08"), class = "factor")), .Names = c("V1",
"V2", "V3"), class = "data.frame", row.names = c(NA, -5L))
见here了解有关data.table
请向我们展示您的期望输出。 –
名称 - 日期之间的差异的平均值 – Veronica
例如对于妮可是24,15,15分钟 所以我想输出数据看起来像妮可 - 18 – Veronica