Display which and how many variables correspond to conditions

Viewed 24

I have a dataset divided into passenger names and their status (suppose, 10 cateogories) like this.

Passenger Status
Peter Captain
Mary Mrs.
Claudette Mrs.
Marius Doc.
Holmes Mr.
... ...

ecc.

In R, how can I display how many passengers are characterised by a specific Status and who? I had in mind a table that represented a situation like "n passengers into the "Mrs." category and their names are "Claudette, Mary ecc."

(I don't need the whole string message, only the number and their names)

How can I do it?

1 Answers

Simply using dplyr

dummy <- read.table(text = "Passenger   Status
Peter   Captain
Mary    Mrs.
Claudette   Mrs.
Marius  Doc.
Holmes  Mr.", header = T)

dummy %>%
  group_by(Status) %>%
  summarise(n = n(),
            names = paste0(Passenger, collapse = ", ")) %>%
  mutate(res = paste0(n, ' passengers into the ', Status, "category and their names are ", names))

  Status      n names           res                                                                   
  <chr>   <int> <chr>           <chr>                                                                 
1 Captain     1 Peter           1 passengers into the Captaincategory and their names are Peter       
2 Doc.        1 Marius          1 passengers into the Doc.category and their names are Marius         
3 Mr.         1 Holmes          1 passengers into the Mr.category and their names are Holmes          
4 Mrs.        2 Mary, Claudette 2 passengers into the Mrs.category and their names are Mary, Claudette
Related