1
votes

I have two time series with hourly resolution now I want to compare the load time series with the capacity time series and count the number of hours when the load is bigger than the capacity. So to know for each hour if there is enough capacity to meet the load. And to calculate the exact difference in cases when there is not enough capacity.

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

I just want to know what is the fastest way to calculate it. I need to compare 10 years of data.

1

1 Answers

1
votes

I would suggest using dplyr which runs considerably fast on large datasets. Check out the following piece of code and also make sure to have a look at the official 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()

And here's the console output of the second operation.

  sufficient  n
1      FALSE  9
2       TRUE 10