count unique combinations of variable values in an R dataframe column

An option with tidyverse where group by 'id', paste the 'status' and get the count

library(dplyr)
library(stringr)
df %>% 
   group_by(id) %>% 
   summarise(status = str_c(status, collapse="")) %>% 
   count(status)
# A tibble: 4 x 2
#  status     n
#  <chr>  <int>
#1 abc        2
#2 b          1
#3 bc         2
#4 bcd        2

Here is a base R option via aggregate

> aggregate(.~status,rev(aggregate(.~id,df,paste0,collapse = "")),length) 
  status id
1    abc  2
2      b  1
3     bc  2
4    bcd  2

Tags:

R

Rle