【发布时间】:2020-11-11 03:50:12
【问题描述】:
我有一个数据框 (top_lang),其中包含国家列表 (country)、每个国家/地区使用的不同语言 (lang) 以及每个国家/地区使用每种语言 (langCountryPop) 的人数。我有每个国家/地区降序排列的 langCountryPop 列,我想提取每个国家/地区的最大数字。
我想要的示例输出是:
x = data.frame("country"= c("American Samoa", "Andorra"), "lang" = c("Samoan", "Catalan"), "langCountryPop" = c(56700, 31000))
但对我数据集中的所有国家/地区重复。
我的尝试是:
top_lang %>% select(country, lang, langCountryPop) %>% arrange(country, max(langCountryPop))
但这并不仅仅输出最高的口语。是否有一个函数可以提取组内的最大值/是否有另一种方法可以做到这一点? 谢谢!
【问题讨论】:
-
top_lang %>% select(country, lang, langCountryPop) %>% group_by(country) %>% filter(langCountryPop==max(langCountryPop))可以帮忙!
标签: r dataframe groupwise-maximum