0

I am wanting to create a count of unique values per id number

For example given the data

test <- data.frame("id" = c(1,1,2,3,3,3), "product" = c("clothes","clothes","home","home","outdoors","outdoors"))

I want the output

id     unique_product_count
1       1
2       1
3       2

I've tried variations such as

test %>% group_by(id) %>% distinct(product)
# A tibble: 4 × 2
# Groups:   id [3]
     id product 
  <dbl> <chr>   
1     1 clothes 
2     2 home    
3     3 home    
4     3 outdoors

But haven't gotten any to work.

Ethan
  • 1
  • Use `n_distinct` instead of `distinct` i.e. `test %>% group_by(id) %>% summarise(unique_product_count = n_distinct(product))` – akrun Dec 06 '21 at 17:54

0 Answers0