Give each group in a data_frame a distinct continous numerical id

I have a Data Frame and wnat to give each group a distinct continous numerical ID. For example i have the following Data Frame:

tibble(Shop = c("B2C", "B2C", "B2B", "B2B"), Product = c("A", "A", "A", "B"))
# A tibble: 4 x 2
  Shop  Products
  <chr> <chr>   
1 B2C   A       
2 B2C   A       
3 B2B   A       
4 B2B   B 

How can i achieve the following output?

# A tibble: 4 x 3
  Shop  Product    ID
  <chr> <chr>   <dbl>
1 B2C   A           1
2 B2C   A           1
3 B2B   A           2
4 B2B   B           2

If uniqueness is what you want, then the following is probably sufficient:

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
fake_dataset <- tibble(Shop = c("B2C", "B2C", "B2B", "B2B"),
                       Product = c("A", "A", "A", "B"))
fake_dataset %>%
    group_by(Shop) %>%
    mutate(ID = group_indices()) %>%
    ungroup()
#> # A tibble: 4 x 3
#>   Shop  Product    ID
#>   <chr> <chr>   <int>
#> 1 B2C   A           2
#> 2 B2C   A           2
#> 3 B2B   A           1
#> 4 B2B   B           1

Created on 2020-02-19 by the reprex package (v0.3.0)

3 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.