vendredi 2 janvier 2015

Ifelse Condition in xts Object

This is a simple one but I dont get the the correct answer myself. I have an xts object with NAs and ones



require(xts)
set.seed(21)
A <- xts(c(NA,NA,NA,1,NA,NA,NA,NA,NA,NA), Sys.Date()-10:1)
colnames(A) <- c("A")


What I am searching for is the ifelse loop that puts a 0 in A exactly 5 days after the 1 occured:



> A
A
2014-12-23 NA
2014-12-24 NA
2014-12-25 NA
2014-12-26 1
2014-12-27 NA
2014-12-28 NA
2014-12-29 NA
2014-12-30 NA
2014-12-31 0
2015-01-01 NA


If by chance there is also a 1 exactly 5 days after the first one (i.e. on 2014-12-31 = 1) then the 1 should be kept.


Aucun commentaire:

Enregistrer un commentaire