~/code/R/

Introduction

  • dplyr 1.0.0. html

  • Data Wrangling Cheatsheet. pdf

## Warning: `tbl_df()` is deprecated as of dplyr 1.0.0.
## Please use `tibble::as_tibble()` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_warnings()` to see where this warning was generated.
## Rows: 1,704
## Columns: 6
## $ country   <fct> Afghanistan, Afghanistan, Afghanistan, Afghanistan, Afghani…
## $ continent <fct> Asia, Asia, Asia, Asia, Asia, Asia, Asia, Asia, Asia, Asia,…
## $ year      <int> 1952, 1957, 1962, 1967, 1972, 1977, 1982, 1987, 1992, 1997,…
## $ lifeExp   <dbl> 28.801, 30.332, 31.997, 34.020, 36.088, 38.438, 39.854, 40.…
## $ pop       <int> 8425333, 9240934, 10267083, 11537966, 13079460, 14880372, 1…
## $ gdpPercap <dbl> 779.4453, 820.8530, 853.1007, 836.1971, 739.9811, 786.1134,…
country continent year lifeExp pop gdpPercap
Afghanistan Asia 1952 28.801 8425333 779.4453
Afghanistan Asia 1957 30.332 9240934 820.8530
Afghanistan Asia 1962 31.997 10267083 853.1007
Afghanistan Asia 1967 34.020 11537966 836.1971
Afghanistan Asia 1972 36.088 13079460 739.9811
Afghanistan Asia 1977 38.438 14880372 786.1134
Afghanistan Asia 1982 39.854 12881816 978.0114
Afghanistan Asia 1987 40.822 13867957 852.3959
Afghanistan Asia 1992 41.674 16317921 649.3414
Afghanistan Asia 1997 41.763 22227415 635.3414

Review: grouping and summarizing

## `summarise()` ungrouping output (override with `.groups` argument)
continent avg_lifeExp
Africa 48.86533
Americas 64.65874
Asia 60.06490
Europe 71.90369
Oceania 74.32621

Meet “do”

country continent year lifeExp pop gdpPercap
Algeria Africa 2007 72.301 33333216 6223.3675
Angola Africa 2007 42.731 12420476 4797.2313
Argentina Americas 2007 75.320 40301927 12779.3796
Bolivia Americas 2007 65.554 9119152 3822.1371
Afghanistan Asia 2007 43.828 31889923 974.5803
Bahrain Asia 2007 75.635 708573 29796.0483
Albania Europe 2007 76.423 3600523 5937.0295
Austria Europe 2007 79.829 8199783 36126.4927
Australia Oceania 2007 81.235 20434176 34435.3674
New Zealand Oceania 2007 80.204 4115771 25185.0091
country continent year lifeExp pop gdpPercap
Morocco Africa 2002 69.615 31167783 3258.496
Ecuador Americas 2002 74.173 12921234 5773.045
Thailand Asia 2002 68.564 62806748 5913.188
Greece Europe 2002 78.256 10603863 22514.255
country continent year lifeExp pop gdpPercap
Ecuador Americas 2002 74.173 12921234 5773.045
Greece Europe 2002 78.256 10603863 22514.255
Morocco Africa 2002 69.615 31167783 3258.496
Thailand Asia 2002 68.564 62806748 5913.188
## tibble [5 × 2] (S3: rowwise_df/tbl_df/tbl/data.frame)
##  $ continent: Factor w/ 5 levels "Africa","Americas",..: 1 2 3 4 5
##  $ range    :List of 5
##   ..$ : num [1:2] 23.6 76.4
##   ..$ : num [1:2] 37.6 80.7
##   ..$ : num [1:2] 28.8 82.6
##   ..$ : num [1:2] 43.6 81.8
##   ..$ : num [1:2] 69.1 81.2
##  - attr(*, "groups")= tibble [5 × 1] (S3: tbl_df/tbl/data.frame)
##   ..$ .rows: list<int> [1:5] 
##   .. ..$ : int 1
##   .. ..$ : int 2
##   .. ..$ : int 3
##   .. ..$ : int 4
##   .. ..$ : int 5
##   .. ..@ ptype: int(0)