Grouped Summary With `purrr::map()`

Feb 2018 · 550 words · 3 minutes read R DataViz

COPY / PASTE HERE:

mtcars %>% split(.$cyl) %>% map(summary)

This is a quick one and sourced from this SO answer but I kept googling it so wanted to just put it up here.

require(dplyr)
require(purrr)

mtcars %>% split(.$cyl) %>% map(summary)
## $`4`
##       mpg             cyl         disp              hp        
##  Min.   :21.40   Min.   :4   Min.   : 71.10   Min.   : 52.00  
##  1st Qu.:22.80   1st Qu.:4   1st Qu.: 78.85   1st Qu.: 65.50  
##  Median :26.00   Median :4   Median :108.00   Median : 91.00  
##  Mean   :26.66   Mean   :4   Mean   :105.14   Mean   : 82.64  
##  3rd Qu.:30.40   3rd Qu.:4   3rd Qu.:120.65   3rd Qu.: 96.00  
##  Max.   :33.90   Max.   :4   Max.   :146.70   Max.   :113.00  
##       drat             wt             qsec             vs        
##  Min.   :3.690   Min.   :1.513   Min.   :16.70   Min.   :0.0000  
##  1st Qu.:3.810   1st Qu.:1.885   1st Qu.:18.56   1st Qu.:1.0000  
##  Median :4.080   Median :2.200   Median :18.90   Median :1.0000  
##  Mean   :4.071   Mean   :2.286   Mean   :19.14   Mean   :0.9091  
##  3rd Qu.:4.165   3rd Qu.:2.623   3rd Qu.:19.95   3rd Qu.:1.0000  
##  Max.   :4.930   Max.   :3.190   Max.   :22.90   Max.   :1.0000  
##        am              gear            carb      
##  Min.   :0.0000   Min.   :3.000   Min.   :1.000  
##  1st Qu.:0.5000   1st Qu.:4.000   1st Qu.:1.000  
##  Median :1.0000   Median :4.000   Median :2.000  
##  Mean   :0.7273   Mean   :4.091   Mean   :1.545  
##  3rd Qu.:1.0000   3rd Qu.:4.000   3rd Qu.:2.000  
##  Max.   :1.0000   Max.   :5.000   Max.   :2.000  
## 
## $`6`
##       mpg             cyl         disp             hp       
##  Min.   :17.80   Min.   :6   Min.   :145.0   Min.   :105.0  
##  1st Qu.:18.65   1st Qu.:6   1st Qu.:160.0   1st Qu.:110.0  
##  Median :19.70   Median :6   Median :167.6   Median :110.0  
##  Mean   :19.74   Mean   :6   Mean   :183.3   Mean   :122.3  
##  3rd Qu.:21.00   3rd Qu.:6   3rd Qu.:196.3   3rd Qu.:123.0  
##  Max.   :21.40   Max.   :6   Max.   :258.0   Max.   :175.0  
##       drat             wt             qsec             vs        
##  Min.   :2.760   Min.   :2.620   Min.   :15.50   Min.   :0.0000  
##  1st Qu.:3.350   1st Qu.:2.822   1st Qu.:16.74   1st Qu.:0.0000  
##  Median :3.900   Median :3.215   Median :18.30   Median :1.0000  
##  Mean   :3.586   Mean   :3.117   Mean   :17.98   Mean   :0.5714  
##  3rd Qu.:3.910   3rd Qu.:3.440   3rd Qu.:19.17   3rd Qu.:1.0000  
##  Max.   :3.920   Max.   :3.460   Max.   :20.22   Max.   :1.0000  
##        am              gear            carb      
##  Min.   :0.0000   Min.   :3.000   Min.   :1.000  
##  1st Qu.:0.0000   1st Qu.:3.500   1st Qu.:2.500  
##  Median :0.0000   Median :4.000   Median :4.000  
##  Mean   :0.4286   Mean   :3.857   Mean   :3.429  
##  3rd Qu.:1.0000   3rd Qu.:4.000   3rd Qu.:4.000  
##  Max.   :1.0000   Max.   :5.000   Max.   :6.000  
## 
## $`8`
##       mpg             cyl         disp             hp       
##  Min.   :10.40   Min.   :8   Min.   :275.8   Min.   :150.0  
##  1st Qu.:14.40   1st Qu.:8   1st Qu.:301.8   1st Qu.:176.2  
##  Median :15.20   Median :8   Median :350.5   Median :192.5  
##  Mean   :15.10   Mean   :8   Mean   :353.1   Mean   :209.2  
##  3rd Qu.:16.25   3rd Qu.:8   3rd Qu.:390.0   3rd Qu.:241.2  
##  Max.   :19.20   Max.   :8   Max.   :472.0   Max.   :335.0  
##       drat             wt             qsec             vs   
##  Min.   :2.760   Min.   :3.170   Min.   :14.50   Min.   :0  
##  1st Qu.:3.070   1st Qu.:3.533   1st Qu.:16.10   1st Qu.:0  
##  Median :3.115   Median :3.755   Median :17.18   Median :0  
##  Mean   :3.229   Mean   :3.999   Mean   :16.77   Mean   :0  
##  3rd Qu.:3.225   3rd Qu.:4.014   3rd Qu.:17.55   3rd Qu.:0  
##  Max.   :4.220   Max.   :5.424   Max.   :18.00   Max.   :0  
##        am              gear            carb     
##  Min.   :0.0000   Min.   :3.000   Min.   :2.00  
##  1st Qu.:0.0000   1st Qu.:3.000   1st Qu.:2.25  
##  Median :0.0000   Median :3.000   Median :3.50  
##  Mean   :0.1429   Mean   :3.286   Mean   :3.50  
##  3rd Qu.:0.0000   3rd Qu.:3.000   3rd Qu.:4.00  
##  Max.   :1.0000   Max.   :5.000   Max.   :8.00
unsplash-logo Benny Jackson