tapply(dat$`lagged Date`, INDEX = dat$Location, FUN = diff(dat$`lagged Date`))

tapply(dat$`lagged Date`, INDEX = dat$Location, FUN = diff(dat$`lagged Date`))

谁能解释一下为什么这不起作用?

tapply(dat$`lagged Date`, INDEX = dat$Location, FUN = diff(dat$`lagged Date`))

我收到以下错误:

Error in match.fun(FUN) : 'diff(dat$lagged Date)' is not a function, character or symbol

structure(list(`lagged Date` = structure(c(1466306880, 1466307060, 
1466307240, 1466307420, 1466307600, 1466307780), class = c("POSIXct", 
"POSIXt"), tzone = "UTC"), Location = c(309, 309, 309, 309, 309, 
309), Duration = c(0, 0, 0, 0, 0, 0), Latitude = c(53.50205667, 
53.501915, 53.50183667, 53.50178833, 53.50184, 53.50186167), 
    Longitude = c(-3.354733333, -3.354096667, -3.353838333, -3.353673333, 
    -3.353711667, -3.353741667), `Number of Records` = c(1, 1, 
    1, 1, 1, 1), Speed = c(0.9, 0, 0, 0, 0, 0), `Sum of Var` = c(38, 
    38, 38, 38, 38, 38), check = c(0, 0, 0, 0, 0, 0)), .Names = c("lagged Date", 
"Location", "Duration", "Latitude", "Longitude", "Number of Records", 
"Speed", "Sum of Var", "check"), row.names = c(NA, -6L), class = c("tbl_df", 
"tbl", "data.frame"))

谢谢!

我不确定你想要实现什么,但仅使用 diff 作为 FUN 部分工作并产生此输出:

tapply(dat$`lagged Date`, INDEX = dat$Location, FUN = diff)

$`309`
Time differences in mins
[1] 3 3 3 3 3

如果要将输出转换为 hours,可以通过仅选择 difftime-list 对象的值并转换这些值来实现:

as.numeric(tapply(dat$`lagged Date`, INDEX = dat$Location, FUN = diff)[[1]], units = "hours")

输出如下所示:

[1] 0.05 0.05 0.05 0.05 0.05