Max() function; want it to return the name of the data not the number

Hi, I'm not sure how best to describe this but basically I'm trying to classify results of football games as H - Home win, D - Draw and A - Away Win via the probabilities of them winning. So for an individual game, i, I have the code:

results[i] = max(H,D,A)

Where H,D and A are all probabilities between 0 and 1. How do I get the Max function to return, for example, H as opposed to its numeric value (if H is the largest number)?

I do not know the type of the object results, but you can use which.max. If you have a vector like x <- c("H", "D", "A") (may be column names of results as well), you can use this:

results[i] = x[which.max(c(H, D, A))]

Hope this helps.


@Jamiet9850, sorry, I made a bad mistake. Hopefully, I fixed it correctly. Please check the edited code. If it fails again, can you please provide a reprex? Otherwise, I cannot test my code.

Hi @Yarnabrina, results is just an empty vector that I want to fill with the results of the matches. It's part of a loop if you'd like me to send you that too as the which.max function just returned "Error in which.max(H, D, A) : unused arguments (D, A)".

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.