Home > Mobile >  Group repeated values with random breaks in R
Group repeated values with random breaks in R

Time:12-14

I have the following data frame called df (dput below):

   group value
1      A     0
2      A    24
3      A     0
4      A    24
5      A     0
6      A     0
7      B     0
8      B    24
9      B     0
10     B     0
11     B    24
12     B     0

I would like to group the repeated values per group when the order is 0->24. Sometimes there is a random 0 with no 24 after. The desired output should look like this:

   group value subgroup
1      A     0        1
2      A    24        1
3      A     0        2
4      A    24        2
5      A     0        3
6      A     0        4
7      B     0        1
8      B    24        1
9      B     0        2
10     B     0        3
11     B    24        3
12     B     0        4

As you can see for rows 5 and 9 there is no 24 after it, that's why they have grouped alone. So I was wondering if anyone knows how to group repeated values with some random breaks in R?


dput df:

df <- structure(list(group = c("A", "A", "A", "A", "A", "A", "B", "B", 
"B", "B", "B", "B"), value = c(0, 24, 0, 24, 0, 0, 0, 24, 0, 
0, 24, 0)), class = "data.frame", row.names = c(NA, -12L))

CodePudding user response:

Looks like the subgroup increments whenever there is a 0 value:

df %>% 
  group_by(group) %>%
  mutate(subgroup = cumsum(value == 0)) %>%
  ungroup()
# # A tibble: 12 × 3
#    group value subgroup
#    <chr> <dbl>    <int>
#  1 A         0        1
#  2 A        24        1
#  3 A         0        2
#  4 A        24        2
#  5 A         0        3
#  6 A         0        4
#  7 B         0        1
#  8 B        24        1
#  9 B         0        2
# 10 B         0        3
# 11 B        24        3
# 12 B         0        4
  • Related