Consider a vector with missing values:
myvec <- c('C', NA, 'test', NA, 'D')
[1] "C" NA "test" NA "D"
How to replace all the elements in the vector before 'test' with, say, 'good' and after it with 'bad'? The outcome should be:
[1] "good" "good" "test" "bad" "bad"
My attempt below succeeds only in replacing everything with 'good', which is not so good:
replace(myvec, is.na(myvec) | myvec!='test', 'good')
[1] "good" "good" "test" "good" "good"