I was working on a project in which if I want to compare the present value with the previous value and return an output 1 if true and 0 if false.
I tried
brv_trx1$'first' <- ifelse(brv_trx1$`Total TRx` != lag(brv_trx1$`Total TRx`),1,0)
This code did not work as expected.
x= c(1,2,2,2,3,4,5,5,5,5,6,7)
I wanted an output similar to this:
x y
1 1
2 1
2 0
2 0
3 1
4 1
5 1
5 0
5 0
After this step I have a decile function
brv_trx1$decvar <- ifelse(brv_trx1$cum != 0 & brv_trx1$first == 1, (11 - ceiling(round((brv_trx1$cum/total) * 10, 4))),
ifelse(brv_trx1$cum != 0 & brv_trx1$first == 0 , lag(brv_trx1$decvar), 0))
For this function, I was getting a lot of NAs.
The output expected was :
Y Dec
1 10
1 10
1 9
0 9
0 9
1 8
0 8
1 8
1 8