Extract dates from vaule xts object

I want to extract the dates of an xts object on which a change in value appears, that is, dates when 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

Desired Result Looks Like It

> from.one.to.zero
[1] "2014-12-29" "2015-01-02"

> from.zero.to.one
[1] "2014-12-31" "2015-01-04"
+4
source share
1 answer

You can 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"
+6
source

Source: https://habr.com/ru/post/1570162/


All Articles