使用 R dplyr 链获取最频繁因子的值
get the value of the most frequent factor using R dplyr chain
我找到了一个简单的解决方案来使用
获取数据框中列的最频繁因子
names(which.max(table(df$column)))
但是如果我想找到链中出现频率最高的因子怎么办。是否有一个简单的代码可以为您提供 'mode' 个因数?
或者有没有一种方法可以将上述代码包含在一个链中?
我已经这样做了,这似乎是在浪费时间。
(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
我找到了一个简单的解决方案来使用
获取数据框中列的最频繁因子names(which.max(table(df$column)))
但是如果我想找到链中出现频率最高的因子怎么办。是否有一个简单的代码可以为您提供 'mode' 个因数?
或者有没有一种方法可以将上述代码包含在一个链中?
我已经这样做了,这似乎是在浪费时间。
(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