Carry value forward in dplyr chain

前端 未结 2 623
梦谈多话
梦谈多话 2020-12-21 08:54

Suppose I have the following column

**CurrentStatus**
Current
NoChange
NoChange
NoChange
NoChange
Late

I want to mutate it so that if the v

2条回答
  •  清歌不尽
    2020-12-21 09:16

    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"
    
    

提交回复
热议问题