-2

I am trying to get data frame in specific format. I have 3 attributes user_id, movie_id, movie_ranking. I want to display best movie_id and ranking for each user based on the maximal ranking.

I guess dplyr library is good for this, but I cannot really get it work.

zx8754
  • 52,746
  • 12
  • 114
  • 209
David Lexa
  • 189
  • 2
  • 13
  • 1
    Provide [reproducible example](http://stackoverflow.com/questions/5963269) and show some coding effort. – zx8754 Aug 19 '16 at 09:45

1 Answers1

-2

With dplyr, we can group by 'user_id', arrange the 'movie_ranking' in descending order and slice the first row.

library(dplyr)
df1 %>%
    group_by(user_id) %>%
    arrange(desc(movie_ranking)) %>%
    slice(1L)

A less efficient option would be using which.max

df1 %>%
    group_by(user_id) %>%
    slice(which.max(movie_ranking))
akrun
  • 874,273
  • 37
  • 540
  • 662