Leonhardt Guass Leonhardt Guass - 1 month ago 8
R Question

How to label clustered groups in a vector?

Data:

set.seed (112098)
op <- data.frame(id=1:100,cluster=rbinom(100,1,0.5))

id cluster
1 1
2 1
3 1
4 0
5 1
6 1
7 0
8 0
9 1


Intended:

id cluster groups
1 1 1
2 1 1
3 1 1
4 0 0
5 1 2
6 1 2
7 0 0
8 0 0
9 1 3


Essentially, every consecutive
1
series forms a group. How could I add the group column in R?

Answer

Here is one option using rleid from data.table

library(data.table)
setDT(op)[, groups := rleid(cluster)*(cluster)
         ][groups!=0, groups := as.integer(factor(groups))]
head(op, 9)
#    id cluster groups
#1:  1       1      1
#2:  2       1      1
#3:  3       1      1
#4:  4       0      0
#5:  5       0      0
#6:  6       1      2
#7:  7       1      2
#8:  8       0      0
#9:  9       1      3