我找到了一个简单的解决方案,可以使用数据框中获取列的最常见因子
names(which.max(table(df$column)))
但是如果我想找到链中最常见的因子怎么办?是否有一个简单的代码可以为您提供因子的“众数”?
或者有没有一种方法可以将上述代码包含在链中?
我就是这样做的,这似乎是浪费时间。
(df %>% group_by(column) %>% summarise(count=n()) %>% arrange(desc(count)))$count[1]
简单的代码将不胜感激,无需提供示例数据。谢谢!
最佳答案
您可以使用 magrittr
中的 %$%
中缀运算符:
df %$% column %>% table %>% which.max %>% names
或者您可以仅使用管道使用以下语法:
df %>% {.$column} %>% table %>% which.max %>% names
或者甚至:
df %>% `$`("column") %>% table %>% which.max %>% names
关于r - 使用 R dplyr 链获取最频繁因子的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43080737/