Reputation: 254
I want to keep track of LO_SEQ_NO before and after it changes currently I am getting after the change. Is there any way I can keep track of the status change
loan_test$lse <-with(loan_test, as.integer(c(FALSE, LO_SEQ_NO[-length(LO_SEQ_NO)] - LO_SEQ_NO[-1]) !=0))
Getting out put as
"LO_SEQ_NO lse
358 0
478 1
478 0
478 0
478 0
But I need output as
"LO_SEQ_NO lse
358 1
478 1
478 0
478 0
478 0
Upvotes: 0
Views: 44
Reputation: 215057
Try check both the lag
and lead
of the column:
df %>% mutate(
lse = as.integer(
LO_SEQ_NO != lag(LO_SEQ_NO, default=first(LO_SEQ_NO)) |
LO_SEQ_NO != lead(LO_SEQ_NO, default=last(LO_SEQ_NO))
)
)
# LO_SEQ_NO lse
#1 358 1
#2 478 1
#3 478 0
#4 478 0
#5 478 0
Upvotes: 1