Carry value forward in dplyr chain

Viewed 484

Suppose I have the following column

**CurrentStatus**
Current
NoChange
NoChange
NoChange
NoChange
Late

I want to mutate it so that if the value is "NoChange" it uses the prior value.

I tried:

myDF %>% mutate(CurrentStatus = ifelse(CurrentStatus == "NoChange", lag(CurrentStatus), CurrentStatus)

That doesn't seem to work -- I think it's because it does a vectorized calculation so it looks at all the lags at the same time. I need it to "roll forward". I was wondering what's the most efficient way to do this without a for loop. I specifically want to avoid a for loop as there are some grouping variables not shown that I need to be mindful of.

Thanks!

2 Answers

You could use something like this:

rfwd<-function(value,trigger)
{
  c("",value)[cummax(seq_along(value)*(trigger))+1]
}

and your answer would be rfwd(CurrentStatus,CurrentStatus!="NoChange")

> rfwd(LETTERS,seq_along(LETTERS)%%10==0)
 [1] ""  ""  ""  ""  ""  ""  ""  ""  ""  "J" "J" "J" "J" "J" "J" "J" "J" "J" "J" "T" "T" "T" "T" "T" "T" "T"

Related