I have a vector of 0
s and 1
s and want to identify the indices where a string of 0
s is surrounded by 1
s. If the number of 0
s between the 1
s is lower or equal than 5, I want to change these zeros to 1
s.
Here is an example:
> x <- c(0,0,0,1,1,1,0,0,0,1,1,0,0,0,0,0,0,1,1,1,1)
In positions 7,8, and 9, I have only three zeros, and thus these need to be changed to 1. The other zeros are more than 5, and thus need not to be changed.
The resulting vector should look like this:
> x_converted <- c(0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,0,1,1,1,1)
I am doing this with a for
loop and if else
statement, but I am sure there must be a faster way to do this.
Thank you.
inverse.rle()
function! – Pearly