티스토리 뷰

Data Science/R package

dplyr_summarise()

jsBae 2022. 5. 4. 00:58
728x90

집단별로 요약하기

> exam %>% 
+   summarise(mean_math=mean(math))
  mean_math
1     57.45
> exam %>% 
+   group_by(class) %>% 
+   summarise(mean_math=mean(math))
# A tibble: 5 × 2
  class mean_math
  <int>     <dbl>
1     1      46.2
2     2      61.2
3     3      45  
4     4      56.8
5     5      78
> exam %>% 
+   group_by(class) %>% 
+   summarise(mean_math=mean(math),
+             sum_math=sum(math),
+             median_math=median(math),
+             n=n())
# A tibble: 5 × 5
  class mean_math sum_math median_math     n
  <int>     <dbl>    <int>       <dbl> <int>
1     1      46.2      185        47.5     4
2     2      61.2      245        65       4
3     3      45        180        47.5     4
4     4      56.8      227        53       4
5     5      78        312        79       4
> mpg %>% 
+   group_by(manufacturer, drv) %>% 
+   summarise(mean_cty=mean(cty)) %>% 
+   head(10)
`summarise()` has grouped output by 'manufacturer'. You can override using the `.groups` argument.
# A tibble: 10 × 3
# Groups:   manufacturer [5]
   manufacturer drv   mean_cty
   <chr>        <chr>    <dbl>
 1 audi         4         16.8
 2 audi         f         18.9
 3 chevrolet    4         12.5
 4 chevrolet    f         18.8
 5 chevrolet    r         14.1
 6 dodge        4         12  
 7 dodge        f         15.8
 8 ford         4         13.3
 9 ford         r         14.8
10 honda        f         24.4
728x90
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
반응형