我有一個帶有游戲、類別、是否單人游戲以及評級的數據框。我添加了一個新的專欄,其中包含每個單人/雙人分組中每個類別的最高評分方面的最佳游戲。
category <- c("Party","Adventure","Puzzle","Party","Adventure","Puzzle","Party","Adventure","Puzzle","Party","Adventure","Puzzle","Party","Adventure","Puzzle","Party","Adventure","Puzzle")
solo <- c("solo","solo","solo","double","double","double","solo","solo","solo","double","double","double","solo","solo","solo","double","double","double")
game <- c("Game1","Game2","Game3","Game1","Game2","Game3","Game1","Game2","Game3","Game1","Game2","Game3","Game1","Game2","Game3","Game1","Game2","Game3")
rating <- c(8,7,6,5,3,3,2,1,10,3,4,5,6,3,2,1,3,1)
df <- as.data.frame(rating)
df$game <- game
df$solo <- solo
df$category <- category
df <- df %>%
group_by(category, solo) %>%
mutate(best_game = max(rating, na.rm = TRUE)) %>%
ungroup
我的問題是,我如何添加一個列,使游戲對應于best_game中的最高評分,而不是每個單人組/雙人組和類別組的最高評分(best_game?
示例輸出。預期產出是我希望發生的事情
rating game solo category best_game **(expected_output)**
5 Game1 double Party 5 Game1
3 Game1 double Party 5 Game1
只需包含一個station,將
game
變量子集為rating == max(rating)