Home > Net >  Assign numbers to groups in dplyr R
Assign numbers to groups in dplyr R

Time:07-28

Example data:

example_data <-
  data.frame(value = c(1,3,4,6,7,8,4,6,9,0),
             group = c("Not applicable",
                       "Large group",
                       "Large group",
                       "Not applicable",
                       "Group of 1",
                       "Group of 1",
                       "Large group",
                       "Large group",
                       "Group of 1",
                       "Not applicable"))

I would like to assign group numbers, starting with 1, to groups, and zeroes to "Not applicable" values, preferably with dplyr.

How do I assign the same group number to each Large group, but different numbers to each Group of 1?

Desired output:

   value          group group_number
1      1 Not applicable            0
2      3    Large group            1
3      4    Large group            1
4      6 Not applicable            0
5      7     Group of 1            2
6      8     Group of 1            3
7      4    Large group            4
8      6    Large group            4
9      9     Group of 1            5
10     0 Not applicable            0

Solution from my previous question:

example_data %>%
  mutate(group_number = data.table::rleid(group)* (group != 'Not applicable'),
         group_number = dense_rank(group_number) - 1)

Gives me:

   value          group group_number
1      1 Not applicable            0
2      3    Large group            1
3      4    Large group            1
4      6 Not applicable            0
5      7     Group of 1            2
6      8     Group of 1            2
7      4    Large group            3
8      6    Large group            3
9      9     Group of 1            4
10     0 Not applicable            0

I would like each Group of 1 to have a different number.

CodePudding user response:

example_data %>%
  mutate(gr = data.table::rleid(group, cumsum(group == 'Group of 1')),
         gr = dense_rank(gr * (group != 'Not applicable')) - 1)

# A tibble: 10 x 3
   value group             gr
   <dbl> <chr>          <dbl>
 1     1 Not applicable     0
 2     3 Large group        1
 3     4 Large group        1
 4     6 Not applicable     0
 5     7 Group of 1         2
 6     8 Group of 1         3
 7     4 Large group        4
 8     6 Large group        4
 9     9 Group of 1         5
10     0 Not applicable     0
  • Related