foo <- c("a","a",NA,"b","a","a","b","b")
How to replace “b” by whatever if previous element is NA ?
foo[foo=="b" & "previous-element"==NA] <- "whatever"
So expected output would be :
result <- c("a","a",NA,"whatever","a","a","b","b")
So only “b” (many in real datas) preceded by NA would be changed.
Thank’s for helping !
A simple solution:
Update:
A solution with
rollapplyfrom thezoopackage: