0

If I have the max value from a column in a data frame, how can I know which row this is from?

I did max(data$women1990) data is the name of the data frame and women 1990 the required column. Is there some way I can revers to find out what row the max value I got actually is from?

1 Answers1

0

You can do:

which.max(data$women1990)

To get the row number, which you can then use:

row.names(data)[which.max(data$women1990)]

To get the name of the row.

For more general purpose code (when you're not looking for the maximum, but a certain value), use:

which(data$women1990==max(data$women1990)) # Put desired value after the ==
Yang Li
  • 462
  • 1
  • 8
  • 21
  • 1
    "Put desired value after the ==", unless you are searching for a numeric, then see http://stackoverflow.com/questions/2769510/numeric-comparison-difficulty-in-r – m-dz Feb 16 '17 at 21:20