jeudi 24 septembre 2020

I need help in an if else statement in R until a certain condition is met

I am trying to create a code that does the following: when the value of z is greater than 2 then assign the value 1 until the value of z crosses zero and when the value of z is less than -2 then assign the value -1 until z crosses zero else give zeros. This is a small example but of course I don't want this result.

ifelse(z>2,1,ifelse(z<(-2),(-1),0))```
[1]  0  0  0  1  1  1  1  0  0  0  0  0  0  0  0 -1 -1  0  0  0  0  0
I want the result like this
[1] 0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,-1,-1,-1,0,0,0,0
By the way the data I use is in XTS format if it helps.
Thanks in advance

Aucun commentaire:

Enregistrer un commentaire