【问题标题】:Display which and how many variables correspond to conditions显示与条件对应的变量和数量
【发布时间】:2021-11-25 12:13:50
【问题描述】:
我有一个数据集,分为乘客姓名和他们的状态(假设,10 个类别),如下所示。
| Passenger |
Status |
| Peter |
Captain |
| Mary |
Mrs. |
| Claudette |
Mrs. |
| Marius |
Doc. |
| Holmes |
Mr. |
| ... |
... |
ecc.
在 R 中,如何显示有多少乘客以特定状态为特征以及谁?
我想到了一个表格,它代表“n 名乘客进入“夫人”类别,他们的名字是“Claudette, Mary ecc”。
(我不需要完整的字符串消息,只需要数字和他们的名字)
我该怎么做?
【问题讨论】:
标签:
r
dataset
classification
categories
data-manipulation
【解决方案1】:
只需使用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