【问题标题】:Average number of seconds between two time observations两次观察之间的平均秒数
【发布时间】:2016-10-12 13:21:45
【问题描述】:

我有一个来自 xts 对象的不规则时间索引。我需要找到两次观察之间的平均秒数。这是我的示例数据:

dput(tt)
structure(c(1371.25, NA, 1373.95, NA, NA, 1373, NA, 1373.95, 
1373.9, NA, NA, 1374, 1374.15, NA, 1374, 1373.85, 1372.55, 1374.05, 
1374.15, 1374.75, NA, NA, 1375.9, 1374.05, NA, NA, NA, NA, NA, 
NA, NA, 1375, NA, NA, NA, NA, NA, 1376.35, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, 1376.25, NA, 1378, 1376.5, NA, NA, NA, 1378, 
1378, NA, NA, 1378.8, 231.9, 231.85, NA, 231.9, 231.85, 231.9, 
231.8, 231.9, 232.6, 231.95, 232.35, 232, 232.1, 232.05, 232.05, 
232.05, 231.5, 231.3, NA, NA, 231.1, 231.1, 231.1, 231, 231, 
230.95, 230.6, 230.6, 230.7, 230.6, 231, NA, 231, 231, 231.45, 
231.65, 231.4, 231.7, 231.3, 231.25, 231.25, 231.4, 231.4, 231.85, 
231.75, 231.5, 231.55, 231.35, NA, 231.5, 231.5, NA, 231.5, 231.25, 
231.15, 231, 231, 231, 231.05, NA), .Dim = c(60L, 2L), .indexCLASS = c("POSIXct", 
"POSIXt"), tclass = c("POSIXct", "POSIXt"), .indexTZ = "Asia/Calcutta", tzone = "Asia/Calcutta", index = structure(c(1459482299, 
1459482301, 1459482302, 1459482303, 1459482304, 1459482305, 1459482306, 
1459482307, 1459482309, 1459482310, 1459482311, 1459482312, 1459482314, 
1459482315, 1459482316, 1459482317, 1459482318, 1459482319, 1459482320, 
1459482321, 1459482322, 1459482323, 1459482324, 1459482326, 1459482328, 
1459482329, 1459482330, 1459482331, 1459482332, 1459482336, 1459482337, 
1459482338, 1459482339, 1459482342, 1459482344, 1459482346, 1459482347, 
1459482348, 1459482349, 1459482590, 1459482591, 1459482594, 1459482595, 
1459482596, 1459482597, 1459482598, 1459482599, 1459482602, 1459482603, 
1459482604, 1459482609, 1459482610, 1459482611, 1459482612, 1459482613, 
1459482618, 1459482619, 1459482620, 1459482622, 1459482628), tzone = "Asia/Calcutta", tclass = c("POSIXct", 
"POSIXt")), .Dimnames = list(NULL, c("A", "B")), class = c("xts", 
"zoo"))

这是我的尝试:

difftime(index(tt),index(lag.xts(tt, k=1)), units=c("auto"))
Time differences in secs
 [1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
attr(,"tclass")
[1] "POSIXct" "POSIXt"

非常感谢任何帮助。

编辑:

根据答案,我制作了以下代码。 该代码旨在计算 A 和 B 每天的平均秒数。

但代码采用 tt 的索引而不是 A 或 B,因此 A 和 B 的结果是相同的。

fun.time= function(x) mean(diff(time(x)))
df.time<-do.call(rbind, lapply(split(tt, "days"), FUN=function (x) {do.call(cbind, lapply(as.list(x), fun.time))})) 


dput(df.time)
structure(c(5.57627118644068, 5.57627118644068), .Dim = 1:2, .Dimnames = list(
    NULL, c("A", "B")))

【问题讨论】:

  • 试试这个attributes(tt)$index %&gt;% diff %&gt;% mean

标签: r xts difftime


【解决方案1】:

首先创建一些超过一天的测试数据,tt2。我们使用 tt 在 tt 来自问题的地方形成它。定义两个函数:

  • mean_diff_time 从其参数中删除 NA,然后将其转换为数值,取其平均差异。
  • 将其参数转换为 Date 类的日期

最后使用 aggregate.zoo 我们按日期聚合它,将 mean_diff_time 应用于每个日期组。

library(xts)

# create test input tt2 having >1 day (tt is from question)
tt2 <- tt
time(tt2) <- time(tt) + seq(1, 24*60*60, length = 60)

mean_diff_time <- function(x) mean(diff(as.numeric(time(na.omit(x)))))
dates <- function(x) as.Date(format(x))

aggregate(tt2, dates, mean_diff_time, coredata = FALSE)
##                       A        B
##     2016-04-01 3029.006 1648.939
##     2016-04-02 5416.096 1632.957

更新

根据新功能修改了答案。

【讨论】:

  • 我可以先求和再求均值吗?
  • @runjumpify。请澄清。
  • 我已经检查过了。并且代码很好。请查看编辑。
  • @runjumpify, format(x) where x is POSIXct 给出一个没有时区的字符串,因此对其应用任何函数的结果不能依赖于时区。查看format(time(tt2))time(tt2)。当然,如果 format() 没有根据您想要的时区转换为字符,那么即使这也可能不是您想要的解决方案;但是,我发现通常它确实以您想要的方式工作。 R News 4/1 中的 R Help Desk 文章对此进行了更多讨论。 r-project.org/doc/Rnews/Rnews_2004-1.pdf
  • 那就对了。您可以通过打印 tt2 并打印 split(tt2, "days") 来检查它是否以您想要的方式拆分。
【解决方案2】:

为了补充 G.Grothendieck 的答案,您还可以使用 mean(diff(index(tt))) 将结果作为 difftime 对象返回:

> mean(diff(index(tt)))
Time difference of 5.576271 secs

或简单地mean(diff(.index(tt))) 以获取数字结果:

> mean(diff(.index(tt)))
[1] 5.576271

编辑:

> lapply(tt, function(x){mean(diff(.index(x[!is.na(x)])))})
$A
[1] 14.95455

$B
[1] 6.211538

【讨论】:

  • 谢谢。请查看问题的编辑部分。
猜你喜欢
  • 2023-03-05
  • 2015-10-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-28
  • 2015-04-14
  • 2022-10-05
  • 1970-01-01
相关资源
最近更新 更多