使用tidyverse进行简单的数据处理:
盘一盘Tidyverse| 筛行选列之select,玩转列操作
盘一盘Tidyverse| 只要你要只要我有-filter 筛选行
Tidyverse|数据列的分分合合,一分多,多合一
Tidyverse| XX_join :多个数据表(文件)之间的各种连接
本次介绍变量汇总以及分组汇总。
一 summarize汇总
 汇总函数 summarise(),可以将数据框折叠成一行  ,多与group_by()结合使用
summarise完成指定变量的汇总统计均值,标准差,最小值,个数和逻辑值
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常用函数:
mean(), median()sd(), IQR(), mad()min(), max(), quantile()first(), last(), nth(),n(), n_distinct()any(), all()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.199333summarise_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二 group_by 分组汇总
group_by() 和 summarise() 的组合构成了使用 dplyr 包时最常用的操作之一:分组摘要
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          50iris %>%
    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当与数值型函数一同使用时, 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                    1https://r4ds.had.co.nz/
书籍:《R数据科学》