I have a vector of numbers and want to check whether there is any zero that is surrounded by two identical numbers e.g.:
x <- c(3,0,3,4,5,2,0,1,3)
any(x == 0 & lag(x) == lead(x))
TRUE
The problem is however that if the vector starts or ends with a 0, I will get a NA, because then either the lag or lead will be NA.
x <- c(0,3,4,5,2,0,1,3)
any(x == 0 & lag(x) == lead(x))
NA
In this case I still want to return a FALSE. Does anyone know how to fix this perhaps with out using the lead- and lag-functions?
Thanks for your help!