【问题标题】:Extract min/max by group in R在R中按组提取最小值/最大值
【发布时间】:2020-10-05 11:22:30
【问题描述】:

(使用 Iris 实现重现性)

我想通过 Petal.Width 计算最小/最大行并按 R 中的 Species 分组。我已经使用两种方法做到了,我想了解是否有更好的方法(最好是 tidyverse),也请注意因为关系答案两者可能有所不同。如果这两种方法有任何错误,请更正。

方法 1

library(tidyverse)

iris %>% 
 group_by(Species) %>% 
  slice_max(Petal.Width, n = 1, with_ties=FALSE) %>% 
  rbind(
iris %>% 
 group_by(Species) %>% 
  slice_min(Petal.Width, n = 1, with_ties=FALSE)) 

方法 2

iris %>% 
  group_by(Species) %>% 
  arrange(Petal.Width) %>% 
  filter(row_number() %in% c(1,n()))

【问题讨论】:

    标签: r dplyr tidyverse


    【解决方案1】:

    这是使用summarise(across()) 的方法:

    library(dplyr)
    iris %>%
      group_by(Species) %>%
      summarise(across(.cols = Petal.Width, 
                       .fns = list(min = min, max = max), 
                       .names = "{col}_{fn}"))
    
    `summarise()` ungrouping output (override with `.groups` argument)
    # A tibble: 3 x 3
      Species    Petal.Width_min Petal.Width_max
      <fct>                <dbl>           <dbl>
    1 setosa                 0.1             0.6
    2 versicolor             1               1.8
    3 virginica              1.4             2.5
    

    您可以通过这种方式轻松找到数据集中每个数值变量的最小值和最大值:

    iris %>%
      group_by(Species) %>%
      summarise(across(where(is.numeric), 
                       .fns = list(min = min, max = max), 
                       .names = "{col}_{fn}"))
    `summarise()` ungrouping output (override with `.groups` argument)
    # A tibble: 3 x 9
      Species    Sepal.Length_min Sepal.Length_max Sepal.Width_min Sepal.Width_max Petal.Length_min Petal.Length_max Petal.Width_min Petal.Width_max
      <fct>                 <dbl>            <dbl>           <dbl>           <dbl>            <dbl>            <dbl>           <dbl>           <dbl>
    1 setosa                  4.3              5.8             2.3             4.4              1                1.9             0.1             0.6
    2 versicolor              4.9              7               2               3.4              3                5.1             1               1.8
    3 virginica               4.9              7.9             2.2             3.8              4.5              6.9             1.4             2.5
    

    【讨论】:

      【解决方案2】:

      您也可以使用slice,如下所示:

      iris %>%
        group_by(Species) %>%
        slice(which.min(Petal.Width),
              which.max(Petal.Width))
      

      输出:

      # A tibble: 6 x 5
      # Groups:   Species [3]
        Sepal.Length Sepal.Width Petal.Length Petal.Width Species   
               <dbl>       <dbl>        <dbl>       <dbl> <fct>     
      1          5           3.5          1.6         0.6 setosa    
      2          5.9         3.2          4.8         1.8 versicolor
      3          6.3         3.3          6           2.5 virginica 
      4          4.9         3.1          1.5         0.1 setosa    
      5          4.9         2.4          3.3         1   versicolor
      6          6.1         2.6          5.6         1.4 virginica 
      

      【讨论】:

        【解决方案3】:

        使用aggregate

        aggregate(Petal.Width ~ Species, iris, function(x) c(min=min(x), max=max(x)))
        #      Species Petal.Width.min Petal.Width.max
        # 1     setosa             0.1             0.6
        # 2 versicolor             1.0             1.8
        # 3  virginica             1.4             2.5
        

        【讨论】:

        • 还有aggregate(Petal.Width ~ Species, iris, function(x) range(x))
        猜你喜欢
        • 1970-01-01
        • 2019-05-18
        • 2018-09-17
        • 2019-04-04
        • 1970-01-01
        • 2023-03-20
        • 1970-01-01
        • 2014-10-17
        • 1970-01-01
        相关资源
        最近更新 更多