I want to extract the dates of an xts object on which a change in value appears, i.e. the dates on which the value of A changes from one to zero or from zero to one:
require(xts)
A <- xts(c(1,1,0,0,1,1,0,0,1,1), Sys.Date()-10:1)
colnames(A) <- c("A")
> A
A
2014-12-27 1
2014-12-28 1
2014-12-29 0
2014-12-30 0
2014-12-31 1
2015-01-01 1
2015-01-02 0
2015-01-03 0
2015-01-04 1
2015-01-05 1
The desired result Looks like this
> from.one.to.zero
[1] "2014-12-29" "2015-01-02"
> from.zero.to.one
[1] "2014-12-31" "2015-01-04"
You could try
index(A[diff(A)<0])
#[1] "2014-12-31" "2015-01-04"
index(A[diff(A)==1])
#[1] "2014-12-29" "2015-01-02"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With