I have a question on the lag function which I am unable to solve in R. I have a variable in a dataframe 'V3' which is a time series of a very large data file. The 'resultV4' is what I want to accomplish (see the code snippet).
- I want my resultV4 to be -1 in the first row, based on V3.
- In the 2nd row of V3 there is a zero, so I want the value of resultV4 to be -1 (the value of the 1st row)
- In the 3rd row of V3 there is a zero, so I want the value of resultV4 to be -1 (the value of the 1st row)
When the value of V3 changes, in this case to 1 in the 6th row, I want resultV4 to be 1. The 7th row is another 0, so I want this to be the value of the 6th row in V3, which is 1. And so on..
V3<-c(-1,0,0,0,0,1,0,0,0,0,-1,0,0,0,0,-1,0,0)
resultV4<-c(-1,-1,-1,-1,-1,1,1,1,1,1,-1,-1,-1)
df<-cbind(V3,resultV4)
Thanks in advance for any suggestions.
Cheers,
PCdL