I do not down-vote, generally, but this question is basic, reveals no investigation, is somewhat replicated and the solution is easily found elsewhere.
There are several ways to achieve this.
being d your data.frame. retrieve the row with the most played song:
d[d$playCount == max(d$playCount), ]
For most played by user, try this
d <- data.frame(userId = rep(seq(1:5),2) ,
songId = letters[1:10],
playCount = c(10:19))
> d
userId songId playCount
1 1 a 10
2 2 b 11
3 3 c 12
4 4 d 13
5 5 e 14
6 1 f 15
7 2 g 16
8 3 h 17
9 4 i 18
10 5 j 19
d2<- d[order(-d$playCount), ]
dout <- d2[!duplicated(d2$userId), ]
> dout
userId songId playCount
10 5 j 19
9 4 i 18
8 3 h 17
7 2 g 16
6 1 f 15
I really don't understand the down-vote. The approach is correct and is fast, almost as fast as dplyr. Try it with a 1000000 rows data frame
df <- data.frame(userId = rep(seq(1:5),100000) ,
songId = rep(letters[1:10], 100000),
playCount = runif(1000000,10,20))
using @Henrik dplyr approach
system.time(df %.%
group_by(userId) %.%
filter(
playCount == max(playCount)))
Source: local data frame [5 x 3]
Groups: userId
userId songId playCount
1 2 b 19.99995
2 5 j 19.99982
3 1 f 19.99981
4 4 d 19.99995
5 3 h 19.99999
user system elapsed
0.08 0.02 0.09
and using Hadley approach
df2<- df[order(-df$playCount), ]
dout <- df2[!duplicated(df2$userId), ]
> dout
userId songId playCount
671528 3 h 19.99999
466824 4 d 19.99995
185512 2 b 19.99995
249190 5 j 19.99982
455746 1 f 19.99981
system.time(dout <- df2[!duplicated(df2$userId), ])
user system elapsed
0.13 0.00 0.12
Now I'd suggest you to up-vote two brilliant approaches, from Hadley here and from Gavin Simpson here.