数据分析

tidyverse|数据分析常规操作-分组汇总(sumamris

2020-07-06  本文已影响0人  生信补给站

| 本文首发于“生信补给站” https://mp.weixin.qq.com/s/tQt0ezYJj3H7x3aWZmKVEQ

一 summarise 汇总

汇总函数 summarize(),可以将数据框折叠成一行 ,多与group_by()结合使用

1.1 summarize完成指定变量的汇总

统计均值,标准差,最小值,个数和逻辑值

library(dplyr)
iris %>%
    summarise(mean(Petal.Length), #无命名
              sd_pet_len = sd(Petal.Length,na.rm = TRUE), #命名
              min_pet_len = min(Petal.Length),
              n = n(),
             any(Sepal.Length > 5))

#  mean(Petal.Length) sd_pet_len min_pet_len   n any(Sepal.Length > 5)
#1              3.758   1.765298           1 150                  TRUE

常用函数:

1.2 , summarise_if完成一类变量的汇总

iris %>%
    summarise_if(is.numeric, ~ mean(., na.rm = TRUE))

#  Sepal.Length Sepal.Width Petal.Length Petal.Width
#1     5.843333    3.057333        3.758    1.199333

1.3,summarise_at完成指定变量的汇总

summarise_at配合vars,可以更灵活的筛选符合条件的列,然后进行汇总

iris %>%
    summarise_at(vars(ends_with("Length"),Petal.Width),
    list(~mean(.), ~median(.)))

#  Sepal.Length_mean Petal.Length_mean Petal.Width_mean Sepal.Length_median Petal.Length_median
#1          5.843333             3.758         1.199333                 5.8                4.35
#  Petal.Width_median
#1                1.3

[图片上传失败...(image-a0776e-1594047144950)]

二 结合group_by 汇总

group_by() 和 summarize() 的组合构成了使用 dplyr 包时最常用的操作之一:分组摘要

2.1 按照Species分组,变量汇总

iris %>%
    group_by(Species) %>%
    summarise(avg_pet_len = mean(Petal.Length),
              sd_pet_len = sd(Petal.Length),
              min_pet_len = min(Petal.Length),
              first_pet_len = first(Petal.Length),
             n_pet_len = n())

# A tibble: 3 x 6
#  Species    avg_pet_len sd_pet_len min_pet_len first_pet_len n_pet_len
#  <fct>            <dbl>      <dbl>       <dbl>         <dbl>     <int>
#1 setosa            1.46      0.174         1             1.4        50
#2 versicolor        4.26      0.470         3             4.7        50
#3 virginica         5.55      0.552         4.5           6          50

2.2 计数

iris %>%
    group_by(Species) %>%
    summarise( n_pet_len = n(),
              noNA_n_pet_len =  sum(!is.na(Petal.Length)),
              Petal.Length_uniq_n = n_distinct(Petal.Length)
             )
# A tibble: 3 x 4
#  Species    n_pet_len noNA_n_pet_len Petal.Length_uniq_n
#  <fct>          <int>          <int>               <int>
#1 setosa            50             50                   9
#2 versicolor        50             50                  19
#3 virginica         50             50                  20

除此之外,还可以用dplyr的count函数进行计数:

iris %>%
    count(Species)

# A tibble: 3 x 2
#  Species        n
#  <fct>      <int>
#1 setosa        50
#2 versicolor    50
#3 virginica     50

2.3 逻辑值的计数和比例

当与数值型函数一同使用时, TRUE 会转换为 1, FALSE 会转换为 0。

这使得 sum() 和 mean() 非常适用于逻辑值: **sum(x) 可以找出 x 中 TRUE 的数量, mean(x) 则可以找出比例 **

iris %>%
    group_by(Species) %>%
    summarise( n_pet_len = n(),
              noNA_n_pet_len =  sum(!is.na(Petal.Length)),
              Petal.Length_uniq_n = n_distinct(Petal.Length),
              Petal.Length_uniq_n2 = sum(n_distinct(Petal.Length) >= 20)
             )

# A tibble: 3 x 5
#  Species    n_pet_len noNA_n_pet_len Petal.Length_uniq_n Petal.Length_uniq_n2
#  <fct>          <int>          <int>               <int>                <int>
#1 setosa            50             50                   9                    0
#2 versicolor        50             50                  19                    0
#3 virginica         50             50                  20                    1

参考资料:

https://r4ds.had.co.nz/

书籍:《R数据科学》

上一篇下一篇

猜你喜欢

热点阅读