【问题标题】:Ordering a table that was created from a factor variable in r订购从 r 中的因子变量创建的表
【发布时间】:2020-05-28 22:12:51
【问题描述】:

好的,我有一个数据框 df,其中有一列:艺术家姓名。

        artist_name
1       Linkin Park
2         Lady Gaga
3        Pink Floyd
4       Linkin Park
5         Lady Gaga
6         Lady Gaga

艺术家姓名是一个因子变量,要查看每个艺术家在此数据框中出现的次数,我会说:

表(df$artist_name)

唯一的问题是,在我的项目中,我有 17,000 行(以及数千名艺术家),我只想查看表格中出现次数最多的艺术家。因为有这么多不同的艺术家,我无法找到最常见的艺术家名字。我知道如果我有一个数据框,我可以使用 order() 函数,但是有没有办法对表格进行排序?非常感谢

【问题讨论】:

    标签: r sorting datatables


    【解决方案1】:

    我们可以在dplyr 中使用count

    library(dplyr)
    df %>% count(artist_name, sort = TRUE) %>% slice(1:10)
    

    或者使用top_n

    df %>% count(artist_name) %>% top_n(10, n)
    

    这将返回前 10 位艺术家,将 10 替换为您想要的任何数字。

    【讨论】:

      【解决方案2】:

      我们可以将sorttable按降序输出,提取第一个元素的names

      names(sort(table(df$artist_name), decreasing = TRUE)[1])
      

      如果我们需要多个,只需将索引中的数字更改为范围

      names(sort(table(df$artist_name), decreasing = TRUE)[1:5])  
      

      注意:不需要包

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-05-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-09-11
        • 2016-07-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多