计算一个变量的5个最高值 [英] Count 5 highest values of a variable
本文介绍了计算一个变量的5个最高值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用R来尝试获取每个用户最喜欢的5首歌曲,并以此来播放最多的歌曲.目前,我有计算最高播放歌曲数量的代码,但我想知道如何为该用户获取下4首最高播放歌曲,假设每个用户至少播放了5首歌曲.我是否必须从数据集中消除最高值并再次运行它,还是有更简单的方法?
I am using R to try to get each user's top 5 favourite songs by which songs they play the most. I currently have code which counts the highest played song but I was wondering how to get the next 4 highest played songs for that user, presuming every user has played at least 5 songs. Would I have to eliminate the highest values from the dataset and run it again or is there an easier way?
write.csv(group_by(mydata,userId) %.%
summarise(favourite=max(playCount)), file="test.csv")
数据示例如下
userId songId playCount
A 568r 85
A 711g 18
C 34n 18
E 454j 65
D 663a 72
B 35d 84
A 34c 72
A 982s 65
E 433f 11
A 565t 7
推荐答案
您可以使用:
rev(sort(x))[1:n]
以获得向量的最高n
值.如果您想获得n
的唯一唯一值,只需添加对unique()
to get the top n
values of a vector. If you wanted the top n
unique values, just add a call to unique()
rev(sort(unique(x)))[1:n]
这篇关于计算一个变量的5个最高值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文