Search code examples
rdataframemaxrow

Trying to find row associated with max value in dataframe R


Like the title says. I am having trouble. for example I have a 2 column (V1,V2) dataframe with lots of rows, around 300,000. I know that

max(df$V2) 

will give me the max value of that second column. Now that I know my max value, how can I get the entire row associated with that value. Thanks!


Solution

  • You have to write

    df[which.max(df$V2), ]
    

    If more than one row contains the max:

    i <- max(df$V2) 
    df[which(df$V2 == i), ]