【发布时间】:2020-03-09 18:16:39
【问题描述】:
我有一个音乐排行榜数据框 bb1
title artist rank score
1 Thank U, Next Ariana Grande 1 100
2 Without Me Halsey 2 99
3 All I Want For Christmas Is You Mariah Carey 3 98
4 Sicko Mode Travis Scott 4 97
5 Sunflower (Spider-Man: Into The Spider-Verse) Post Malone & Swae Lee 5 96
6 High Hopes Panic! At The Disco 6 95
7 Happier Marshmello & Bastille 7 94
8 Jingle Bell Rock Bobby Helms 8 93
9 Rockin' Around The Christmas Tree Brenda Lee 9 92
10 A Holly Jolly Christmas Burl Ives 10 91
.
.
.
101 Without Me Halsey 1 100
102 Thank U, Next Ariana Grande 2 99
103 Sunflower (Spider-Man: Into The Spider-Verse) Post Malone & Swae Lee 3 98
104 Sicko Mode Travis Scott 4 97
105 High Hopes Panic! At The Disco 5 96
106 Happier Marshmello & Bastille 6 95
107 Girls Like You Maroon 5 Featuring Cardi B 7 94
108 Drip Too Hard Lil Baby & Gunna 8 93
109 ZEZE Kodak Black Featuring Travis Scott & Offset 9 92
110 Better Now Post Malone 10 91
它基本上是每周热门歌曲的汇编。
为了汇总每首歌的“分数”,制作年度前 10 名榜单
我想添加合并每首歌曲的分数并按分数降序排列图表。
所以我尝试了下面的代码
for(i in 1:nrow(bb1)){
if (((bb1[i,1]==bb1[i+1,1])&&(bb1[i,2]==bb1[i+1,2]))){
bb1[i+1,4]<- (bb1[i,4]+bb1[i+1,4])
bb1<-bb1[-c(i),]
i=i+1
}
}
bb1[order(-bb1$score),]
但它并没有像我想要的那样工作。而是弹出这个错误
Error in if (((bb1[i, 1] == bb1[i + 1, 1]) && (bb1[i, 2] == bb1[i + 1, :
missing value where TRUE/FALSE needed
谁能帮帮我?还有其他更有效的方法来制作年度图表吗?
【问题讨论】:
标签: r dataframe merge duplicates