Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Assign numbers to groups in dplyr R

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading