简体   繁体   English

如何提取具有最小值或最大值的行?

[英]How to extract the row with min or max values?

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. R 打印行的索引,例如 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?难道没有更简单的方法可以知道哪个ID与df的特定列的最大值相关吗?

您可以将which.max调用作为子集调用的第一个参数包含在内:

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

A (relatively new) alternative is to use slice_max (or slice_min ) from the tidyverse .一个(相对较新的)替代方法是使用slice_max tidyverseslice_min )。 Using mtcars as example:mtcars为例:

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:请注意, slice_maxslice_min会为您提供在指定列中具有最大值或最小值的所有行(例如调用mtcars[mtcars$mpg == min(mtcars$mpg), ] 。因此,如果您只想要第一行(就像在调用mtcars[which.min(mtcars$mpg), ]中一样),您需要再次切片:

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 .此外, slice还为slice_headslice_tailslice_sample等常见用例提供了更多帮助函数。

您还可以使用子集和最大函数来调用该行:

df[df$Temp == max(df$Temp),]

If you are interested in finding the min/max for certain groups, then you can combine group_by and slice_max in tidyverse.如果您有兴趣找到某些组的最小值/最大值,那么您可以在 tidyverse 中组合 group_by 和 slice_max。 In this example, say you wanted to know the max Temp within each year, then you would do the following:在此示例中,假设您想知道每年的最高温度,那么您将执行以下操作:

df %>% group_by(Temp) %>% slice_max(Temp)

Just another way to maximize the tidyverse packages.只是另一种最大化 tidyverse 包的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM