【问题标题】:R-return the name with the least number of occurencesR-返回出现次数最少的名字
【发布时间】:2016-06-14 15:29:05
【问题描述】:

我需要在我的数据框中找到频率最低的扇区。使用 min 给出了最少的出现次数,但我想获得出现次数最少的相应扇区名称......所以在这种情况下,我希望它打印“消费订书钉”。我不断得到频率而不是实际的扇区名称。有没有办法做到这一点?

谢谢。

sector_count <- count(portfolio, "Sector")
sector_count
                  Sector freq
1 Consumer Discretionary    5
2       Consumer Staples    1
3            Health Care    2
4            Industrials    3
5 Information Technology    4

min(sector_count$freq)
[1] 1

【问题讨论】:

  • with(sector_count,Sector[which.min(freq)])
  • count 不是基本 R 函数。本网站的标准做法是在此处的代码中使用此类函数之前包含 library(dplyr) 之类的行。
  • @BenBolker 谢谢,这也很有效。

标签: r count min


【解决方案1】:

你想要的

sector_count$Sector[which.min(sector_count$freq)]

which.min(sector_count$freq) 函数选择找到最小值的索引或行。然后将扇区计数$扇区向量作为相应值的子集。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-29
    • 2018-08-17
    • 1970-01-01
    • 2017-03-18
    • 1970-01-01
    相关资源
    最近更新 更多