Skip to contents

Use the Gapminder data in various forms to practice different styles of grouped computation.

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(purrr)
library(tibble)

## group_by() + summarize()
gap_simple %>% 
  group_by(country) %>%
  summarize(cor = cor(lifeExp, year))
#> # A tibble: 142 × 2
#>    country       cor
#>    <fct>       <dbl>
#>  1 Afghanistan 0.974
#>  2 Albania     0.954
#>  3 Algeria     0.993
#>  4 Angola      0.942
#>  5 Argentina   0.998
#>  6 Australia   0.990
#>  7 Austria     0.996
#>  8 Bahrain     0.983
#>  9 Bangladesh  0.995
#> 10 Belgium     0.997
#> # … with 132 more rows

## nest() + map_*() inside mutate()
gap_nested %>%
  mutate(cor = data %>% map_dbl(~ cor(.x$lifeExp, .x$year)))
#> # A tibble: 142 × 4
#>    country     continent data                cor
#>    <fct>       <fct>     <list>            <dbl>
#>  1 Afghanistan Asia      <tibble [12 × 4]> 0.974
#>  2 Albania     Europe    <tibble [12 × 4]> 0.954
#>  3 Algeria     Africa    <tibble [12 × 4]> 0.993
#>  4 Angola      Africa    <tibble [12 × 4]> 0.942
#>  5 Argentina   Americas  <tibble [12 × 4]> 0.998
#>  6 Australia   Oceania   <tibble [12 × 4]> 0.990
#>  7 Austria     Europe    <tibble [12 × 4]> 0.996
#>  8 Bahrain     Asia      <tibble [12 × 4]> 0.983
#>  9 Bangladesh  Asia      <tibble [12 × 4]> 0.995
#> 10 Belgium     Europe    <tibble [12 × 4]> 0.997
#> # … with 132 more rows

## nest() + rowwise() + mutate()
gap_nested %>% 
  rowwise() %>% 
  mutate(cor = cor(data$lifeExp, data$year))
#> # A tibble: 142 × 4
#> # Rowwise: 
#>    country     continent data                cor
#>    <fct>       <fct>     <list>            <dbl>
#>  1 Afghanistan Asia      <tibble [12 × 4]> 0.974
#>  2 Albania     Europe    <tibble [12 × 4]> 0.954
#>  3 Algeria     Africa    <tibble [12 × 4]> 0.993
#>  4 Angola      Africa    <tibble [12 × 4]> 0.942
#>  5 Argentina   Americas  <tibble [12 × 4]> 0.998
#>  6 Australia   Oceania   <tibble [12 × 4]> 0.990
#>  7 Austria     Europe    <tibble [12 × 4]> 0.996
#>  8 Bahrain     Asia      <tibble [12 × 4]> 0.983
#>  9 Bangladesh  Asia      <tibble [12 × 4]> 0.995
#> 10 Belgium     Europe    <tibble [12 × 4]> 0.997
#> # … with 132 more rows

## split + map_*()
gap_split %>% 
  map_dbl(~ cor(.x$lifeExp, .x$year)) %>% 
  head()
#> Afghanistan     Albania     Algeria      Angola   Argentina   Australia 
#>   0.9735051   0.9542420   0.9925307   0.9422392   0.9977816   0.9897716

## split + map_*() + tibble::enframe()
gap_split %>% 
  map_dbl(~ cor(.x$lifeExp, .x$year)) %>% 
  enframe()
#> # A tibble: 142 × 2
#>    name        value
#>    <chr>       <dbl>
#>  1 Afghanistan 0.974
#>  2 Albania     0.954
#>  3 Algeria     0.993
#>  4 Angola      0.942
#>  5 Argentina   0.998
#>  6 Australia   0.990
#>  7 Austria     0.996
#>  8 Bahrain     0.983
#>  9 Bangladesh  0.995
#> 10 Belgium     0.997
#> # … with 132 more rows