I have the following R data.table, which is composed of only one column:
library(data.table)
DT <- data.table(first_column = c(0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0))
> DT
first_column
1: 0
2: 0
3: 0
4: 1
5: 1
6: 1
7: 0
8: 0
9: 1
10: 1
11: 0
12: 0
13: 0
14: 0
15: 1
16: 1
17: 1
18: 1
19: 1
20: 0
21: 0
... ...
The binary column first_column
is composed of "clusters" of consecutive ones.
I would like to turn each preceding 0 for each cluster and turn this into a 1. Somehow, one checks for a 1
, and then change the preceding 0 into 1.
EDIT: To be more clear, the pattern 0001110011000011111...
would become 0011110111000111111...