显示哪些变量和多少变量对应条件

Display which and how many variables correspond to conditions

我有一个数据集,分为乘客姓名和他们的状态(假设有 10 个类别),就像这样。

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

ecc.

在 R 中,我如何显示有多少乘客具有特定状态以及谁? 我想到了一个 table 表示这样的情况:“n 名乘客进入“夫人”类别,他们的名字是“Claudette,Mary ecc。”

(我不需要整个字符串消息,只需要数字和他们的名字)

我该怎么做?

只需使用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