Search code examples
rrow-number

Find row name of a Table given max value


I have a table, tble from a dataframe, dfas below:

tble<-table(df$eye_color)

So it produces values as follows:

tble
    Brown        Black      Green      Blue
     7          10          2           1 

I want to define a variable that returns the row name with the max value in the table. For this example, it would be Black.


Solution

  • Strange, I did not find a duplicate when I search "[r] max of a table". But I remember seeing something similar.

    names(tble)[which.max(tble)]