Consider the following simple example:
df <- data.frame(score1 = c(10, 12, 19, 13),
score2 = c(13, 10, 16, 12),
score3 = c(10, 10, 19, 15),
score4 = c(11, 14, 17, 15))
I want to:
(i) work out for each row of data, which score is the highest
(ii) in the event of a tie for the highest score, favour lower score numbers (eg in a tie between score1
and score2
, score1
wins)
I can do this:
df$max_score <- pmax(df$score1, df$score2, df$score3, df$score4)
df$winning_score_name <- ifelse(df$score1 >= df$max_score,
"score1",
ifelse(df$score2 >= df$max_score,
"score2",
ifelse(df$score3 >= df$max_score,
"score3",
"score4")))
and df$winning_score_name
gives me:
[1] "score2" "score4" "score1" "score3"
...but in the real world example I'm working with (which has many score columns), this would be very hard to read & to maintain. Is there a more concise way to code this pls?
Thank you.