본문 바로가기
R

(R) make a summary - group_by(),summarise()

by jangpiano 2020. 7. 22.
반응형

<summarise()> 


>library(ggplot2)              ------------------for mpg

>library(dplyr)                ------------------for summarise()


>mpg%>%summarise(mean_cty=mean(cty))



# A tibble: 1 x 1

mean_cty

<dbl>

1     16.9




<group_by() + summarise()>


A summary of a group(drv) by mean_cty


>drv_cty<-mpg%>%group_by(drv)%>%summarise(mean_cty=mean(cty))


> drv_cty


# A tibble: 3 x 2

  drv   mean_cty

  <chr>    <dbl>

1 4         14.3

2 f         20.0

3 r         14.1


A summary of groups of drv, manufacturer (4,f,r - Audi,Chevrolet, ...) by mean_cty 


> drv_manu_cty<-mpg%>%group_by(drv,manufacturer)%>%summarise(mean_cty=mean(cty))


> drv_manu_cty


# A tibble: 22 x 3

# Groups:   drv [3]

   drv   manufacturer mean_cty

   <chr> <chr>           <dbl>

 1 4     audi             16.8

 2 4     chevrolet        12.5

 3 4     dodge            12  

 4 4     ford             13.3

 5 4     jeep             13.5

 6 4     land rover       11.5

 7 4     mercury          13.2

 8 4     nissan           13.8

 9 4     subaru           19.3

10 4     toyota           14.9

  .

  .

  .......


Three summaries for groups of drv (4,f,r) by mean_cty, median_cty, min_cty 


> drv_cty<-mpg%>%group_by(drv)%>%summarise(mean_cty=mean(cty),median_cty=median(cty),min_cty=min(cty))


> drv_cty


# A tibble: 3 x 4

  drv   mean_cty median_cty min_cty

  <chr>    <dbl>      <dbl>   <int>

1 4         14.3         14       9

2 f         20.0         19      11

3 r         14.1         15      11


A summary for groups of drv (4,f,r) by the number of each drv (n=n())


> the_number_of_each_drv<-mpg%>%group_by(drv)%>%summarise(n=n())


> the_number_of_each_drv

# A tibble: 3 x 2

  drv       n

  <chr> <int>

1 4       103

2 f       106

3 r        25



<Application >


> library(ggplot2)

> library(dplyr)

> diamonds_1<-diamonds%>%group_by(carat)%>%summarise(mean_price=mean(price))

> plot(diamonds_1$carat,diamonds_1$mean_price,type="l")




A summary after excluding NA


> msleep2<-na.omit(msleep)

> msleep3<-msleep2%>%group_by(vore)%>%summarise(mean_t=mean(sleep_total),mean_rem=mean(sleep_rem))

`summarise()` ungrouping output (override with `.groups` argument)

> View(msleep3)


반응형