R-return出现次数最少的名字

R-return the name with the least number of occurences

我需要在我的数据框中找到频率最低的扇区。使用 min 给出了最少的出现次数,但我想获得出现次数最少的相应扇区名称......所以在这种情况下,我希望它打印 "consumer staples"。我一直在获取频率而不是实际的扇区名称。有办法吗?

谢谢。

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

你想要

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

which.min(sector_count$freq)函数选择找到最小值的索引或行。 sector_count$Sector 向量然后是相应值的子集。