R语言学习-Group By的实现

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wlt9037/article/details/74421916
library(dplyr)
data <- data.frame(year = rep(2016:2017,6),month = seq(1:12),sales=rep(c(10,20,30,40),3))
   year month sales
1  2016     1    10
2  2017     2    20
3  2016     3    30
4  2017     4    40
5  2016     5    10
6  2017     6    20
7  2016     7    30
8  2017     8    40
9  2016     9    10
10 2017    10    20
11 2016    11    30
12 2017    12    40
planes <- group_by(data, year)
delay <- summarise(planes,
                   count = n(),                #个数
                   max_mon = max(month),       #最大值
                   min_mon = min(month),       #最小值
                   avg_sales = mean(sales),    #平均值
                   sum_sales = sum(sales))     #求和
# A tibble: 2 x 6
   year count max_mon min_mon avg_sales sum_sales
  <int> <int>   <dbl>   <dbl>     <dbl>     <dbl>
1  2016     6      11       1        20       120
2  2017     6      12       2        30       180

阅读更多
换一批

没有更多推荐了,返回首页