[英]Count factor levels inside another factor - R
假设我们有一个包含数百个汽车观察的数据框。 每个观察可以按 model、品牌和国家分组。
我们如何计算每个国家生产了多少型号的汽车?
我试过了:
janitor::tabyl(data, coutry, model)
但是我得到每个国家的 model 的每个观察量。 但是,我正在寻找每个国家/地区的特定国家/地区的模型数量和列表。
例子:
Country n model
Italy 4 Punto, Panda, Mito, Giulietta
Germany 3 Polo, Golf, X5
你可以在 dplyr
df <- data.frame(Country = c("Italy","Italy",
"Italy","Italy","Germany","Germany","Germany"),
Model = c("Punto","Panda",
"Mito","Mito","Polo","Golf","Golf")
)
library(dplyr)
df %>% group_by(Country) %>%
summarise(n = n_distinct(Model),
model = toString(unique(Model)), .groups = 'drop')
# A tibble: 2 x 3
Country n model
<chr> <int> <chr>
1 Germany 2 Polo, Golf
2 Italy 3 Punto, Panda, Mito
由reprex package (v2.0.0) 于 2021 年 5 月 6 日创建
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.