samedi 2 janvier 2021

If condition to give zeros after a number is repeated n times

I have an XTS object of 20 columns with ones, negative ones and zeros. I want to construct an if condition that gives zeros after negative ones or ones reaches a specific number count. for example, if I have a data of 2 columns (variables) and I want to get zeros after 1 or -1 is repeated 15 times:

A<-c(0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1)

B<-c(0,0,0,0,0,1,1,1,1,1,1,0,-1,-1,-1,-1,-1,-1,-1,-1,-1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)

data<-cbind(A,B)

I expect the result to be like data2

A2<-c(0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,0,0,0,0,0,0)

B2<-c(0,0,0,0,0,1,1,1,1,1,1,0,-1,-1,-1,-1,-1,-1,-1,-1,-1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0)

data2<-cbind(A2,B2)

Thanks in Advance

Aucun commentaire:

Enregistrer un commentaire