I would like setdiffbetween successive groups without a loop, if possible, using a data type or a function of the apply family.
Dataframe df:
id group
1 L1 1
2 L2 1
3 L1 2
4 L3 2
5 L4 2
6 L3 3
7 L5 3
8 L6 3
9 L1 4
10 L4 4
11 L2 5
I want to know how many new identifiers exist between consecutive groups. So, for example, if we compare groups 1 and 2, there are two new identifiers: L3and L4, therefore, it returns 2 (not setdiffdirectly, but with length()), if we compare group 2 and 3, L5and L6are news identifiers, therefore it returns 2 and etc.
Expected results:
new_id
2
2
2
1
Data:
structure(list(id = structure(c(1L, 2L, 1L, 3L, 4L, 3L, 5L, 6L,
1L, 4L, 2L), .Label = c("L1", "L2", "L3", "L4", "L5", "L6"), class = "factor"),
group = c(1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 5)), class = "data.frame", row.names = c(NA,
-11L), .Names = c("id", "group"))