rr-rownames

Return corresponding row name instead of data in r


apologies in advance, I am a beginner trying to learn. I have a dataframe which I have performed the max() function to return the maximum number of a particular column however I would like to return the corresponding row name of the max number as opposed to the max number itself.

Any help much appreciated

Many Thanks


Solution

  • You can subset the row.names vector with the index of the max value of the column.

    df <- data.frame(
      x = 1:100
    )
    
    row.names(df)[which(df$x == max(df$x, na.rm = TRUE))]
    
    # "100"