R排序最常见

时间:2013-02-14 11:32:09

标签: r

这可能非常简单但是,我有一个短语的矢量,其中一些重复,其中一些不重要,我想要一个独特的短语列表,按最常见的短语排序。

e.g。

vec <- c("hello","hi","hi","greetings","good day", "hi", "hello", "good day","good morning","hello","good day")

sort(unique(vec))

[1] "good day"     "good morning" "greetings"    "hello"        "hi"  

我希望首先是“hi”然后是“hello”然后是“good day”等......

1 个答案:

答案 0 :(得分:2)

只需使用sort(table(vec))

sort(table(vec), decreasing=TRUE)
# vec
#     good day        hello           hi good morning    greetings 
#            3            3            3            1            1