gpt4 book ai didi

r - 两次观察之间的平均秒数

转载 作者:行者123 更新时间:2023-12-05 08:56:30 27 4
gpt4 key购买 nike

我有一个来自 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"

非常感谢任何帮助。

Edit:

根据答案,我编写了以下代码。 该代码用于计算 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")))

最佳答案

首先创建一些超过一天的测试数据,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

更新

根据新功能修改了答案。

关于r - 两次观察之间的平均秒数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39999744/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com