Finding appearances in multiple groups

library(dplyr)

# toy data
zone <- rep(c("A", "B", "C"), each = 3)
shop <- c(1, 2, 3,  1, 5,   7, 3,   8, 9)
df <- tibble(zone = zone, shop = shop)
df
#> # A tibble: 9 x 2
#>   zone   shop
#>   <chr> <dbl>
#> 1 A         1
#> 2 A         2
#> 3 A         3
#> 4 B         1
#> 5 B         5
#> 6 B         7
#> 7 C         3
#> 8 C         8
#> 9 C         9

How can I identify the shops that belong to multiple zones? In the above example, shop 1 belongs to zone A and zone B; shop 3 belongs to zone A and zone C.

Here is how you can count the distinct values in a group, so that would tell you that shop A belongs to two groups, for example:

library(dplyr)

# toy data
zone <- rep(c("A", "B", "C"), each = 3)
shop <- c(1, 2, 3,  1, 5,   7, 3,   8, 9)
df <- tibble(zone = zone, shop = shop)
df
#> # A tibble: 9 x 2
#>   zone   shop
#>   <chr> <dbl>
#> 1 A         1
#> 2 A         2
#> 3 A         3
#> 4 B         1
#> 5 B         5
#> 6 B         7
#> 7 C         3
#> 8 C         8
#> 9 C         9
  
df %>% 
    group_by(shop) %>% 
    summarize(n = n_distinct(zone))
#> # A tibble: 7 x 2
#>    shop     n
#> * <dbl> <int>
#> 1     1     2
#> 2     2     1
#> 3     3     2
#> 4     5     1
#> 5     7     1
#> 6     8     1
#> 7     9     1

Created on 2022-06-20 by the reprex package (v1.0.0)

1 Like

Many thanks! Your solution helped me achieve what I wanted!

library(dplyr)

# toy data
zone <- rep(c("A", "B", "C"), each = 3)
shop <- c(1, 2, 3,  1, 5,   7, 3,   8, 9)
df <- tibble(zone = zone, shop = shop)

df %>% 
  group_by(shop) %>% 
  mutate(n = n_distinct(zone)) %>% 
  filter(n > 1) %>% 
  arrange(shop)
#> # A tibble: 4 x 3
#> # Groups:   shop [2]
#>   zone   shop     n
#>   <chr> <dbl> <int>
#> 1 A         1     2
#> 2 B         1     2
#> 3 A         3     2
#> 4 C         3     2

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.