【问题标题】:tapply(dat$`lagged Date`, INDEX = dat$Location, FUN = diff(dat$`lagged Date`))tapply(dat$`滞后日期`, INDEX = dat$Location, FUN = diff(dat$`滞后日期`))
【发布时间】:2017-05-23 10:34:55
【问题描述】:

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

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

我收到以下错误:

match.fun(FUN) 中的错误:'diff(dat$lagged Date)' 不是 函数、字符或符号

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"))

谢谢!

【问题讨论】:

    标签: r tapply


    【解决方案1】:

    我不确定您想要实现什么,但仅使用 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
    

    【讨论】:

    • 是的,但时差是秒而不是小时。我想在几个小时内拥有它。有可能吗?
    • 嗯,这取决于你的输出应该是什么样子。查看我的编辑。
    猜你喜欢
    • 1970-01-01
    • 2019-07-25
    • 1970-01-01
    • 1970-01-01
    • 2020-12-29
    • 2013-05-15
    • 2020-04-10
    • 2017-01-10
    • 1970-01-01
    相关资源
    最近更新 更多