David Lexa David Lexa - 3 months ago 10
R Question

How to calculate max value for each user and add other attributes

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.


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

df1 %>%
    group_by(user_id) %>%
    arrange(desc(movie_ranking)) %>%

A less efficient option would be using which.max

df1 %>%
    group_by(user_id) %>%