如何排序表中最频繁的数据

时间:2019-07-10 17:49:43

标签: r

如何获取列中最频繁的数据

数据:

column 1 | column 2

"hi"     | "user1"

"text"   | "user1"

"text2"  | "user1"

"halo"   | "user2"

"hi"     | "user2"

"test"   | "user3"

"ok"     | "user1"

预期结果:

Users Column | Count Column

"user1"      | "4"

"user2"      | "2"

"user1"      | "1"

1 个答案:

答案 0 :(得分:0)

这是另一种解决方案。

library(dplyr)
column_1=c("hi", "text", "text2", "halo", "hi", "test", "ok" )
column_2 =c("user1", "user1", "user1", "user2", "user2","user3", "user1")
df = data.frame(column_1, column_2)

group_by(df, column_2) %>%
  summarise(count=n()) %>%
  head()

输出:

column_2 count
  <fct>    <int>
1 user1        4
2 user2        2
3 user3        1