比较两个时间序列

时间:2016-02-09 08:26:04

标签: r time-series

我有两个小时分辨率的时间序列现在我想比较负载时间序列和容量时间序列,并计算负载大于容量时的小时数。因此要知道每小时是否有足够的容量来满足负载。并且在没有足够容量的情况下计算确切的差异。

library(xts)
load<-c(81,81,82,98,81,67,90,92,75,78,83,83,83,43,97,92,72,85,62)
capacity<-c(78,97,78,65,45,98,67,109,78,109,52,42,97,87,83,90,99,89,125)
time1<-seq(from=as.POSIXct("2013-01-01 00:00"),to=as.POSIXct("2013-01-01     18:00"),by="hour")
dat0<-data.frame(load,capacity)
df1<-xts(dat0,order.by=time1)

df1
                     load capacity
2013-01-01 00:00:00   81       78
2013-01-01 01:00:00   81       97
2013-01-01 02:00:00   82       78
2013-01-01 03:00:00   98       65
2013-01-01 04:00:00   81       45
2013-01-01 05:00:00   67       98
2013-01-01 06:00:00   90       67
2013-01-01 07:00:00   92      109
2013-01-01 08:00:00   75       78
2013-01-01 09:00:00   78      109
2013-01-01 10:00:00   83       52
2013-01-01 11:00:00   83       42
2013-01-01 12:00:00   83       97
2013-01-01 13:00:00   43       87
2013-01-01 14:00:00   97       83
2013-01-01 15:00:00   92       90
2013-01-01 16:00:00   72       99
2013-01-01 17:00:00   85       89
2013-01-01 18:00:00   62      125

我只想知道计算它的最快方法是什么。我需要比较10年的数据。

1 个答案:

答案 0 :(得分:1)

我建议使用 dplyr ,它在大型数据集上运行得相当快。查看以下代码,并确保查看官方Introduction to dplyr

library(dplyr)

## difference between capacity and load
dat0 %>% 
  mutate(diff = capacity - load) -> dat1

## count hours with sufficient capacity
dat1 %>%
  count(sufficient = diff >= 0) %>%
  data.frame()

这是第二个操作的控制台输出。

  sufficient  n
1      FALSE  9
2       TRUE 10
相关问题