I am doing some cyclical analysis.
I have Variable X, which if true if in the state of contraction, and false otherwise
X
##[1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
....
which I changed into 0's and 1's by
X2<-as.ts(X*1)
Then I have a date sequence.
td
## [1] "2000-01-31" "2000-02-29" "2000-03-31" "2000-04-30" "2000-05-31" "2000-06-30"
....
which i then used 'zoo' to index X2
with order td.
library(zoo)
na_ts = zoo(x=X2, order.by=td)
Now is my question. I would want to identify the dates when the value changes, and count how long the series has stayed as 1 and 0.
So desired outcome:
start end type duration
2000-01-31 - 2001-05-31 contraction 17 months
2001-06-30 - 2004-05-31 expansion ....
Would anybody help me please? Many thanks in advance.