1

我有大量的电压读数数据集。我希望确定电压高于阈值的不同时间段的频率。因此,对于电压子集(> = 2V),我希望计算连续周期的长度,其中电压基于 1 秒的采样间隔。请参阅下面的示例数据:

library(chron)
volts=c(2,3,4,5,6,2,2,3,4,5,5,5)
t=chron(times=c("23:03:20", "23:03:21", "23:03:22", "23:03:23","23:03:24","23:03:25","04:01:50","04:01:51","04:01:52","04:01:53","04:01:54","04:01:55"))
data=data.frame(volts,"time"=t)

这个例子有两个感兴趣的系列,一个从 23:03:20 到 23:03:25,一个从 04:01:50 到 04:01:55。

我希望计算这些周期的持续时间和大量数据的平均电压,条件是周期之间有 30 秒的间隔,才能被认为是离散的。我怀疑我的答案可能取决于动物园,我欢迎提出建议。

4

3 回答 3

2

在输出中使用与@BenBolker 相同的名称:

library(data.table)
dt = data.table(data)

dt[, time := as.ITime(time)] # easier to deal with

dt[, list(meanvolts = mean(volts), duration = time[.N] - time[1], n = .N),
     by = list(period = 1 + c(0, cumsum(abs(diff(time)) >= 30)))]
#   period meanvolts duration n
#1:      1  3.666667 00:00:05 6
#2:      2  4.000000 00:00:05 6
于 2013-07-30T21:43:01.203 回答
1

这里有一个解决方案xts包。一般我们使用period.apply函数来处理这样的时间序列过程。在这里,实际上,我仅endpoints用于创建拆分器索引(每 30 秒),然后使用 classic 循环sapply

library(xts)
## creating the `xts` objects.
x.z <- xts(data$volts,
           as.POSIXct(strptime(data$time,format='%H:%M:%S')))

INDEX <- endpoints(x.z,'secs',30)
xx <- sapply(1:(length(INDEX) - 1), function(y) {
  x <- x.z[(INDEX[y] + 1):INDEX[y + 1]]
  data.frame(period=y,
             duration=diff(range(index(x))),
             mm = mean(x),
             len = length(x))
})


t(xx)
     period duration mm       len
[1,] 1      5        4        6  
[2,] 2      5        3.666667 6  

编辑如何endpoints处理时间索引超过日边界的特殊情况?

创建示例:

## creating xts object index
ii <- as.POSIXct(strptime(data$time,format='%H:%M:%S'))
## here I add  day to simulate day boundary
ii[6]  <- as.POSIXct(ii[6] + as.difftime(1,units='days'))

现在我的时间系列看起来像:

    x.z
                    [,1]
2013-07-31 04:01:50    2
2013-07-31 04:01:51    3
2013-07-31 04:01:52    4
2013-07-31 04:01:53    5
2013-07-31 04:01:54    5
2013-07-31 04:01:55    5
2013-07-31 23:59:55    2
2013-07-31 23:59:56    3
2013-07-31 23:59:57    4
2013-07-31 23:59:58    5
2013-07-31 23:59:59    6
2013-08-01 00:00:02    2   ## day boundaries here

应用相同的代码(解决方案的开头),我们得到3 个句点,而不是预期的 2 个

 t(xx)
     period duration mm len
[1,] 1      5        4  6  
[2,] 2      4        4  5  
[3,] 3      0        2  1    ## 2013-08-01 00:00:02    2 
于 2013-07-30T21:41:49.087 回答
1

可能有一个更有效的解决方案zoo,但是如何:

样本数据(为方便起见重复)

library(chron)
dat <- data.frame(volts=c(2,3,4,5,6,2,2,3,4,5,5,5),
  time=chron(times=c("23:03:20", "23:03:21", "23:03:22", 
             "23:03:23","23:03:24","23:03:25",
              "04:01:50","04:01:51","04:01:52","04:01:53",
              "04:01:54","04:01:55")))

分析:

daysecs <- 3600*24
dd <- c(unclass(diff(dat$time))*daysecs)   ## difference in seconds
## classify jumps to new periods, including day boundaries 
## (I haven't tested this carefully!)
new_per <- !((dd>0 & dd<30) | (dd<0 & dd<(-daysecs+30))) 
dat$period <- 1+c(0,cumsum(new_per)) ## a fairly standard trick
library(plyr)
ddply(dat,.(period),summarise,
      meanvolts=mean(volts),
      duration=tail(time,1)-time[1],
      n=length(volts))

结果:

##   period meanvolts duration n
## 1      1  3.666667 00:00:05 6
## 2      2  4.000000 00:00:05 6

plyr特别是不是超快,但是我会在您的数据上尝试一下,看看它是否可以接受,然后再返回并让我们了解它必须有多快(编辑您的问题(“我有 600 万电压测量......”)或发布一个链接到这个的新问题)

于 2013-07-30T21:19:44.450 回答