I have a table that looks like this:
data <- structure(list(group = c(0L, 0L, 1L, 2L), id = c("1", "2", "3",
"4"), m = c("ac1", "ac1", "ac1", "me0"), together = c(FALSE,
FALSE, TRUE, TRUE)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-4L))
I would like to make a column group
that increments when together == FALSE
and stays at the current number when together == TRUE
.
This is my desired output:
# A tibble: 4 x 4
id m together group
<chr> <chr> <lgl> <dbl>
1 1 ac1 FALSE 0
2 2 ac1 FALSE 1
3 3 ac1 TRUE 2
4 4 me0 TRUE 2
I've already tried solutions like R increment by 1 for every change in value column and restart the counter and it's not giving me exactly what I want..
# A tibble: 4 x 4
# Groups: group [3]
id m together group
<chr> <chr> <lgl> <int>
1 1 ac1 FALSE 0
2 2 ac1 FALSE 0
3 3 ac1 TRUE 1
4 4 me0 TRUE 2
See I want group to read 0,1,2,2 in this case. Any ideas? Thank you so much.