我想提取出现值变化的xts对象的日期,即A的值从1变为零或从0变为1的日期:
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
期望的结果看起来像这样
> from.one.to.zero
[1] "2014-12-29" "2015-01-02"
> from.zero.to.one
[1] "2014-12-31" "2015-01-04"
答案 0 :(得分:6)
你可以尝试
index(A[diff(A)<0])
#[1] "2014-12-31" "2015-01-04"
index(A[diff(A)==1])
#[1] "2014-12-29" "2015-01-02"