The question looks simple but I didn't figure out how it can done in R. I want to modify a logical vector depending on patterns of its values. There are two modification steps:
- If there is a single FALSE surrounded by TRUE values, switch it to TRUE.
- If there are less then 3 successive TRUE values, switch them to FALSE.
Everything else should remain as it is. Here's an example:
# input
x = c(FALSE, TRUE, FALSE, FALSE, TRUE, TRUE, FALSE, FALSE, TRUE, TRUE, TRUE,
FALSE, TRUE, TRUE, FALSE, FALSE, TRUE, TRUE, TRUE, TRUE, FALSE)
# output
xo = c(FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, TRUE,
TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, TRUE, TRUE, TRUE, TRUE, FALSE)
cbind(x,xo) is
x xo
[1,] FALSE FALSE
[2,] TRUE FALSE
[3,] FALSE FALSE
[4,] FALSE FALSE
[5,] TRUE FALSE
[6,] TRUE FALSE
[7,] FALSE FALSE
[8,] FALSE FALSE
[9,] TRUE TRUE
[10,] TRUE TRUE
[11,] TRUE TRUE
[12,] FALSE TRUE
[13,] TRUE TRUE
[14,] TRUE TRUE
[15,] FALSE FALSE
[16,] FALSE FALSE
[17,] TRUE TRUE
[18,] TRUE TRUE
[19,] TRUE TRUE
[20,] TRUE TRUE
[21,] FALSE FALSE
I dont want to use a for loop because its slow and I would have to do a lot of if statements.
Is there a better way to get this working?