中文 · PDF 97
#> #> 年 月 日 n_early #> #> 1 2013 1 1 0 #> 2 2013 1 2 3 #> 3 2013 1 3 4 #> 4 2013 1 4 3 #> 5 2013 1 5 3 #> 6 2013 1 6 2 #> # ... 还有 359 行 # 被延误超过一小时的航班比例是多少? not_cancelled %>% group_by(year, month, day) %>% summarize(hour_perc = mean(arr_delay > 60)) #> Source: local data frame [365 x 4] #> Groups: year, month [?] #> #> 年 月 日 hour_perc #> #> 1 2013 1 1 0.0722 #> 2 2013 1 2 0.0851 #> 3 2013 1 3 0.0567 #> 4 2013 1 4 0.0396 #> 5 2013 1 5 0.0349 #> 6 2013 1 6 0.0470 #> # ... 还有 359 行
按多个变量分组
当你按多个变量分组时,每次汇总都会剥离一层分组。这使得逐步向上汇总数据集变得非常容易:
daily <- group_by(flights, year, month, day) (per_day <- summarize(daily, flights = n())) #> Source: local data frame [365 x 4] #> Groups: year, month [?] #> #> 年 月 日 flights #> #> 1 2013 1 1 842 #> 2 2013 1 2 943 #> 3 2013 1 3 914 #> 4 2013 1 4 915 #> 5 2013 1 5 720 #> 6 2013 1 6 832 #> # ... 还有 359 行