Find total distinct names

library(dplyr)
df <- tibble(
  customer = c("Mike", "Mike", "John", "Pedro", "Bill", "Tim"),
  old_address = rep(c("P", "Q"), each = 3),
  correct_address = rep(c("X", "Y"), each = 3)
)
df
#> # A tibble: 6 x 3
#>   customer old_address correct_address
#>   <chr>    <chr>       <chr>          
#> 1 Mike     P           X              
#> 2 Mike     P           X              
#> 3 John     P           X              
#> 4 Pedro    Q           Y              
#> 5 Bill     Q           Y              
#> 6 Tim      Q           Y

How can I mutate total distinct number of customer who are still reporting old_address?

Here is my desired output:

# A tibble: 2 x 3
  old_address correct_address distinct_customer
  <chr>       <chr>                       <dbl>
1 P           X                               2
2 Q           Y                               3
df %>% 
  group_by(old_address, correct_address) %>% 
  summarize(distinct_customer = n_distinct(customer), .groups = "drop")

This topic was automatically closed 7 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.