Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extracting Dates from xts object based on vaule

Tags:

date

r

zoo

xts

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"
like image 950
Pat Avatar asked Jan 06 '15 14:01

Pat


1 Answers

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"
like image 54
akrun Avatar answered Nov 15 '22 07:11

akrun