Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract the row with min or max values?

Tags:

r

max

min

With a dataframe like this one:

        ID  Year    Temp    ph
1       P1  1996    11.3    6.80
2       P1  1996    9.7     6.90
3       P1  1997    9.8     7.10
...
2000    P2  1997    10.5    6.90
2001    P2  1997    9.9     7.00
2002    P2  1997    10.0    6.93

if I want to know where the max value is I type:

which.max(df$Temp)

and R print the index of the row, for example 665.

So, if I want to read and extract the column with all the related values, I have to type:

df[665, ]

Isn't there a simpler way to know which ID is related to the max value of a specific column of the df?

like image 867
matteo Avatar asked Oct 18 '13 12:10

matteo


People also ask

How do you find Max rows in R?

Row wise maximum in R dataframe using rowMaxs() rowMaxs() function takes up column 2 to 4 and finds row wise maximum value.

How do you find the max and min of a Dataframe?

The min() method returns a Series with the minimum value of each column. By specifying the column axis ( axis='columns' ), the max() method searches column-wise and returns the minimum value for each row.

How do you find the minimum and maximum value in R?

We can find the minimum and the maximum of a vector using the min() or the max() function. A function called range() is also available which returns the minimum and maximum in a two element vector.


3 Answers

You can include your which.max call as the first argument to your subsetting call:

df[which.max(df$Temp),]
like image 72
Joshua Ulrich Avatar answered Sep 24 '22 21:09

Joshua Ulrich


You could also use a subset and max function to call the row:

df[df$Temp == max(df$Temp),]
like image 28
Derwin Brennan Avatar answered Sep 22 '22 21:09

Derwin Brennan


A (relatively new) alternative is to use slice_max (or slice_min) from the tidyverse. Using mtcars as example:

library(tidyverse)
mtcars %>% slice_max(mpg)
#                 mpg cyl disp hp drat    wt qsec vs am gear carb
# Toyota Corolla 33.9   4 71.1 65 4.22 1.835 19.9  1  1    4    1

mtcars %>% slice_min(mpg)
#                      mpg cyl disp  hp drat    wt  qsec vs am gear carb
# Cadillac Fleetwood  10.4   8  472 205 2.93 5.250 17.98  0  0    3    4
# Lincoln Continental 10.4   8  460 215 3.00 5.424 17.82  0  0    3    4

Note that slice_max and slice_min give you all the rows, which have a max or min value in the specified column (like the call mtcars[mtcars$mpg == min(mtcars$mpg), ]. So if you only want the first row (like in the call mtcars[which.min(mtcars$mpg), ]), you need to slice once again like:

mtcars %>% slice_min(mpg) %>% slice(1)
#                     mpg cyl disp  hp drat   wt  qsec vs am gear carb
# Cadillac Fleetwood 10.4   8  472 205 2.93 5.25 17.98  0  0    3    4

Moreover, slice also offers a few more helper functions for common use cases like slice_head, slice_tail and slice_sample.

like image 20
symbolrush Avatar answered Sep 22 '22 21:09

symbolrush