两次时间观测之间的平均秒数 [英] Average number of seconds between two time observations
问题描述
我有一个来自 xts 对象的不规则时间索引.我需要找到两次观察之间的平均秒数.这是我的示例数据:
I have a irregular time index from an xts object. I need to find the average number of seconds between two time observations. This is the my sample data:
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 每天的平均秒数.
Based on the answers, I have made the following code. The code is meant to calculate mean number of seconds for A and B every day.
但是代码取的是tt的索引而不是A或B,所以A和B的结果是一样的.
But the code takes the index of tt instead of A or B and so the results of A and B is same.
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 来自问题.定义两个函数:
First create some test data that has more than one day, tt2. We use tt to form it where tt is from the question. Define two functions:
- mean_diff_time 从其参数中删除 NA,然后将其转换为数字,并取其平均差异.
- 将其参数转换为 Date 类的日期
最后使用aggregate.zoo,我们按日期对每个日期组应用mean_diff_time进行聚合.
Finally using aggregate.zoo we aggregate it by date applying mean_diff_time to each date group.
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
更新
根据新功能修改了答案.
Update
Have revised answer in light of new features.
这篇关于两次时间观测之间的平均秒数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!