class: center middle main-title section-title-1 # Manipuler des<br>bases de données<br>avec `dplyr` .class-info[ <figure> <img src="img/04/dplyr.png" alt="dplyr" title="dplyr" width="15%"> </figure> ] --- layout: false class: title title-1 section-title-inv-1 .small[ ``` r gapminder ``` ``` ## # A tibble: 1,704 × 6 ## country continent year lifeExp pop gdpPercap ## <fct> <fct> <int> <dbl> <int> <dbl> ## 1 Afghanistan Asia 1952 28.8 8425333 779. ## 2 Afghanistan Asia 1957 30.3 9240934 821. ## 3 Afghanistan Asia 1962 32.0 10267083 853. ## 4 Afghanistan Asia 1967 34.0 11537966 836. ## 5 Afghanistan Asia 1972 36.1 13079460 740. ## 6 Afghanistan Asia 1977 38.4 14880372 786. ## 7 Afghanistan Asia 1982 39.9 12881816 978. ## 8 Afghanistan Asia 1987 40.8 13867957 852. ## 9 Afghanistan Asia 1992 41.7 16317921 649. ## 10 Afghanistan Asia 1997 41.8 22227415 635. ## # ℹ 1,694 more rows ``` ] --- class: title title-1 # tidyverse <figure> <img src="img/02/tidyverse-language.png" alt="tidyverse and language" title="tidyverse and language" width="100%"> </figure> ??? From "Master the Tidyverse" by RStudio --- class: title title-1 # tidyverse .center[ <figure> <img src="img/02/tidyverse.png" alt="The tidyverse" title="The tidyverse" width="50%"> </figure> ] --- class: title title-1 # `dplyr`: verbes pour manipuler des données <table> <tr> <td>Extraire des lignes avec <code>filter()</code></td> <td><img src="img/04/filter.png" alt="filter" title="filter" height="80px"></td> </tr> <tr> <td>Extraire des colonnes avec <code>select()</code></td> <td><img src="img/04/select.png" alt="select" title="select" height="80px"></td> </tr> <tr> <td>Arranger/trier les lignes avec <code>arrange()</code></td> <td><img src="img/04/arrange.png" alt="arrange" title="arrange" height="80px"></td> </tr> <tr> <td>Créer/modifier des colonnes avec <code>mutate()</code></td> <td><img src="img/04/mutate.png" alt="mutate" title="mutate" height="80px"></td> </tr> <tr> <td>Résumer des sous-ensembles avec<br><code>group_by() |> summarize()</code></td> <td><img src="img/04/summarize.png" alt="summarize" title="summarize" height="80px"></td> </tr> </table> --- class: center middle section-title section-title-1 # `filter()` --- layout: false class: title title-1 # `filter()` .box-inv-1[Extraire des lignes selon une condition logique] .pull-left[ ] .pull-right[ - <b><span style="background: #FFDFD1">`DATA`</span></b> = tableau de données à transformer - <b><span style="background: #FFD0CF">`...`</span></b> = Un ou plusieurs tests<br>.small[`filter()` retourne chaque ligne pour lequel le test retourne `TRUE`] ] --- .pull-left[ <table> <thead> <tr> <th style="text-align:left;"> country </th> <th style="text-align:left;"> continent </th> <th style="text-align:left;"> year </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> Afghanistan </td> <td style="text-align:left;"> Asia </td> <td style="text-align:left;"> 1952 </td> </tr> <tr> <td style="text-align:left;"> Afghanistan </td> <td style="text-align:left;"> Asia </td> <td style="text-align:left;"> 1957 </td> </tr> <tr> <td style="text-align:left;"> Afghanistan </td> <td style="text-align:left;"> Asia </td> <td style="text-align:left;"> 1962 </td> </tr> <tr> <td style="text-align:left;"> Afghanistan </td> <td style="text-align:left;"> Asia </td> <td style="text-align:left;"> 1967 </td> </tr> <tr> <td style="text-align:left;"> Afghanistan </td> <td style="text-align:left;"> Asia </td> <td style="text-align:left;"> 1972 </td> </tr> <tr> <td style="text-align:left;"> … </td> <td style="text-align:left;"> … </td> <td style="text-align:left;"> … </td> </tr> </tbody> </table> ] -- .pull-right[ <table> <thead> <tr> <th style="text-align:left;"> country </th> <th style="text-align:left;"> continent </th> <th style="text-align:right;"> year </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> Denmark </td> <td style="text-align:left;"> Europe </td> <td style="text-align:right;"> 1952 </td> </tr> <tr> <td style="text-align:left;"> Denmark </td> <td style="text-align:left;"> Europe </td> <td style="text-align:right;"> 1957 </td> </tr> <tr> <td style="text-align:left;"> Denmark </td> <td style="text-align:left;"> Europe </td> <td style="text-align:right;"> 1962 </td> </tr> <tr> <td style="text-align:left;"> Denmark </td> <td style="text-align:left;"> Europe </td> <td style="text-align:right;"> 1967 </td> </tr> <tr> <td style="text-align:left;"> Denmark </td> <td style="text-align:left;"> Europe </td> <td style="text-align:right;"> 1972 </td> </tr> <tr> <td style="text-align:left;"> Denmark </td> <td style="text-align:left;"> Europe </td> <td style="text-align:right;"> 1977 </td> </tr> </tbody> </table> ] --- class: title title-1 # `filter()` .pull-left[ ] .pull-right[ .box-inv-1[Un signe `=`<br> pour l'assignation d'un argument] .box-inv-1[Deux signes `==`<br>teste l'égalité entre objets<br>.small[(retourne TRUE ou FALSE)]] ] --- class: title title-1 # Tests logiques <table> <tr> <th class="cell-center">Test</th> <th class="cell-left">Signification</th> <th class="cell-center">Test</th> <th class="cell-left">Signification</th> </tr> <tr> <td class="cell-center"><code class="remark-inline-code">x < y</code></td> <td class="cell-left">plus petit que</td> <td class="cell-center"><code class="remark-inline-code">x %in% y</code></td> <td class="cell-left">dans (élément d'un vecteur)</td> </tr> <tr> <td class="cell-center"><code class="remark-inline-code">x > y</code></td> <td class="cell-left">plus grand que</td> <td class="cell-center"><code class="remark-inline-code">is.na(x)</code></td> <td class="cell-left">valeur manquante?</td> </tr> <tr> <td class="cell-center"><code class="remark-inline-code">==</code></td> <td class="cell-left">égale</td> <td class="cell-center"><code class="remark-inline-code">!is.na(x)</code></td> <td class="cell-left">valeurs non manquantes</td> </tr> <tr> <td class="cell-center"><code class="remark-inline-code">x <= y</code></td> <td class="cell-left">plus petit ou égal à</td> </tr> <tr> <td class="cell-center"><code class="remark-inline-code">x >= y</code></td> <td class="cell-left">plus grand ou égal à</td> </tr> <tr> <td class="cell-center"><code class="remark-inline-code">x != y</code></td> <td class="cell-left">différent de</td> </tr> </table> --- class: title title-1 section-title-inv-1 # À votre tour #1: filtrer .box-1[Utilisez `filter()` et des tests logiques pour montrer…] 1. Les données du Canada 2. Toutes les données de pays situés en Océanie (Oceania) 3. Lignes où l'espérance de vie est supérieure à 82 ans
--- .medium[ ``` r filter(gapminder, country == "Canada") ``` ] -- .medium[ ``` r filter(gapminder, continent == "Oceania") ``` ] -- .medium[ ``` r filter(gapminder, lifeExp > 82) ``` ] --- class: title title-1 # Erreur fréquente .pull-left[ .box-inv-1[Utiliser `=` plutôt que `==`] ] -- .pull-right[ .box-inv-1[Mettre entre accolade] ] --- class: title title-1 # `filter()` avec des conditions multiples .box-inv-1[Extrait les lignes qui valident *tous* les énoncés] --- .pull-left[ <table> <thead> <tr> <th style="text-align:left;"> country </th> <th style="text-align:left;"> continent </th> <th style="text-align:left;"> year </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> Afghanistan </td> <td style="text-align:left;"> Asia </td> <td style="text-align:left;"> 1952 </td> </tr> <tr> <td style="text-align:left;"> Afghanistan </td> <td style="text-align:left;"> Asia </td> <td style="text-align:left;"> 1957 </td> </tr> <tr> <td style="text-align:left;"> Afghanistan </td> <td style="text-align:left;"> Asia </td> <td style="text-align:left;"> 1962 </td> </tr> <tr> <td style="text-align:left;"> Afghanistan </td> <td style="text-align:left;"> Asia </td> <td style="text-align:left;"> 1967 </td> </tr> <tr> <td style="text-align:left;"> Afghanistan </td> <td style="text-align:left;"> Asia </td> <td style="text-align:left;"> 1972 </td> </tr> <tr> <td style="text-align:left;"> … </td> <td style="text-align:left;"> … </td> <td style="text-align:left;"> … </td> </tr> </tbody> </table> ] -- .pull-right[ <table> <thead> <tr> <th style="text-align:left;"> country </th> <th style="text-align:left;"> continent </th> <th style="text-align:right;"> year </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> Denmark </td> <td style="text-align:left;"> Europe </td> <td style="text-align:right;"> 2002 </td> </tr> <tr> <td style="text-align:left;"> Denmark </td> <td style="text-align:left;"> Europe </td> <td style="text-align:right;"> 2007 </td> </tr> </tbody> </table> ] --- class: title title-1 # Opérateurs logiques <table> <tr> <th class="cell-center">Opérateur</th> <th class="cell-center">Signification</th> </tr> <tr> <td class="cell-center"><code class="remark-inline-code">a & b</code></td> <td class="cell-center">et</td> </tr> <tr> <td class="cell-center"><code class="remark-inline-code">a | b</code></td> <td class="cell-center">ou</td> </tr> <tr> <td class="cell-center"><code class="remark-inline-code">!a</code></td> <td class="cell-center">pas</td> </tr> </table> --- class: title title-1 # Par défaut, union ("et") .box-inv-1[Commandes équivalentes] --- class: title title-1 section-title-inv-1 # À votre tour #2: filtrer .box-1[Utilisez `filter()` et les opérateurs logiques pour montrer…] 1. Les données du Canada avant 1970 2. Les pays où l'espérance de vie en 2007 est inférieure à 50 3. Les pays hors d'Afrique où l'espérance de vie en 2007 est inférieur à 50
--- ``` r filter(gapminder, country == "Canada", year < 1970) ``` -- ``` r filter(gapminder, year == 2007, lifeExp < 50) ``` -- ``` r filter(gapminder, year == 2007, lifeExp < 50, continent != "Africa") ``` --- class: title title-1 # Erreurs fréquentes .pull-left[ .box-inv-1[Rassembler plusieurs conditions<br>en une seule] .small-code[ ] .small-code[ ] ] -- .pull-right[ .box-inv-1[Utiliser plusieurs tests<br> plutôt que `%in%`] .small-code[ ] .small-code[ ] ] --- class: title title-1 # Syntaxe commune .box-inv-1[Chaque verbe de `dplyr` a la même syntaxe] .box-inv-1[Base de données comme premier argument, retourne une base de données] .pull-left[ ] .pull-right[ - <b><span style="background: #EFB3FF">`VERB`</span></b> = fonction `dplyr`/verbe - <b><span style="background: #FFDFD1">`DATA`</span></b> = base de données à transformer - <b><span style="background: #FFD0CF">`...`</span></b> = commandes pour le verbe ] --- class: title title-1 # `mutate()` .box-inv-1[Créer de nouvelles colonnes] .pull-left[ ] .pull-right[ - <b><span style="background: #FFDFD1">`DATA`</span></b> = base de données à transformer - <b><span style="background: #FFD0CF">`...`</span></b> = colonnes à modifier/créer ] --- .pull-left.small[ <table> <thead> <tr> <th style="text-align:left;"> country </th> <th style="text-align:left;"> year </th> <th style="text-align:left;"> gdpPercap </th> <th style="text-align:left;"> pop </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> Afghanistan </td> <td style="text-align:left;"> 1952 </td> <td style="text-align:left;"> 779.4453145 </td> <td style="text-align:left;"> 8425333 </td> </tr> <tr> <td style="text-align:left;"> Afghanistan </td> <td style="text-align:left;"> 1957 </td> <td style="text-align:left;"> 820.8530296 </td> <td style="text-align:left;"> 9240934 </td> </tr> <tr> <td style="text-align:left;"> Afghanistan </td> <td style="text-align:left;"> 1962 </td> <td style="text-align:left;"> 853.10071 </td> <td style="text-align:left;"> 10267083 </td> </tr> <tr> <td style="text-align:left;"> Afghanistan </td> <td style="text-align:left;"> 1967 </td> <td style="text-align:left;"> 836.1971382 </td> <td style="text-align:left;"> 11537966 </td> </tr> <tr> <td style="text-align:left;"> Afghanistan </td> <td style="text-align:left;"> 1972 </td> <td style="text-align:left;"> 739.9811058 </td> <td style="text-align:left;"> 13079460 </td> </tr> <tr> <td style="text-align:left;"> … </td> <td style="text-align:left;"> … </td> <td style="text-align:left;"> … </td> <td style="text-align:left;"> … </td> </tr> </tbody> </table> ] -- .pull-right.small[ <table> <thead> <tr> <th style="text-align:left;"> country </th> <th style="text-align:right;"> year </th> <th style="text-align:left;"> … </th> <th style="text-align:right;"> gdp </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> Afghanistan </td> <td style="text-align:right;"> 1952 </td> <td style="text-align:left;"> … </td> <td style="text-align:right;"> 6567086330 </td> </tr> <tr> <td style="text-align:left;"> Afghanistan </td> <td style="text-align:right;"> 1957 </td> <td style="text-align:left;"> … </td> <td style="text-align:right;"> 7585448670 </td> </tr> <tr> <td style="text-align:left;"> Afghanistan </td> <td style="text-align:right;"> 1962 </td> <td style="text-align:left;"> … </td> <td style="text-align:right;"> 8758855797 </td> </tr> <tr> <td style="text-align:left;"> Afghanistan </td> <td style="text-align:right;"> 1967 </td> <td style="text-align:left;"> … </td> <td style="text-align:right;"> 9648014150 </td> </tr> <tr> <td style="text-align:left;"> Afghanistan </td> <td style="text-align:right;"> 1972 </td> <td style="text-align:left;"> … </td> <td style="text-align:right;"> 9678553274 </td> </tr> <tr> <td style="text-align:left;"> Afghanistan </td> <td style="text-align:right;"> 1977 </td> <td style="text-align:left;"> … </td> <td style="text-align:right;"> 11697659231 </td> </tr> </tbody> </table> ] --- .pull-left.small[ <table> <thead> <tr> <th style="text-align:left;"> country </th> <th style="text-align:left;"> year </th> <th style="text-align:left;"> gdpPercap </th> <th style="text-align:left;"> pop </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> Afghanistan </td> <td style="text-align:left;"> 1952 </td> <td style="text-align:left;"> 779.4453145 </td> <td style="text-align:left;"> 8425333 </td> </tr> <tr> <td style="text-align:left;"> Afghanistan </td> <td style="text-align:left;"> 1957 </td> <td style="text-align:left;"> 820.8530296 </td> <td style="text-align:left;"> 9240934 </td> </tr> <tr> <td style="text-align:left;"> Afghanistan </td> <td style="text-align:left;"> 1962 </td> <td style="text-align:left;"> 853.10071 </td> <td style="text-align:left;"> 10267083 </td> </tr> <tr> <td style="text-align:left;"> Afghanistan </td> <td style="text-align:left;"> 1967 </td> <td style="text-align:left;"> 836.1971382 </td> <td style="text-align:left;"> 11537966 </td> </tr> <tr> <td style="text-align:left;"> Afghanistan </td> <td style="text-align:left;"> 1972 </td> <td style="text-align:left;"> 739.9811058 </td> <td style="text-align:left;"> 13079460 </td> </tr> <tr> <td style="text-align:left;"> … </td> <td style="text-align:left;"> … </td> <td style="text-align:left;"> … </td> <td style="text-align:left;"> … </td> </tr> </tbody> </table> ] -- .pull-right.small[ <table> <thead> <tr> <th style="text-align:left;"> country </th> <th style="text-align:right;"> year </th> <th style="text-align:left;"> … </th> <th style="text-align:right;"> gdp </th> <th style="text-align:right;"> pop_mil </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> Afghanistan </td> <td style="text-align:right;"> 1952 </td> <td style="text-align:left;"> … </td> <td style="text-align:right;"> 6567086330 </td> <td style="text-align:right;"> 8 </td> </tr> <tr> <td style="text-align:left;"> Afghanistan </td> <td style="text-align:right;"> 1957 </td> <td style="text-align:left;"> … </td> <td style="text-align:right;"> 7585448670 </td> <td style="text-align:right;"> 9 </td> </tr> <tr> <td style="text-align:left;"> Afghanistan </td> <td style="text-align:right;"> 1962 </td> <td style="text-align:left;"> … </td> <td style="text-align:right;"> 8758855797 </td> <td style="text-align:right;"> 10 </td> </tr> <tr> <td style="text-align:left;"> Afghanistan </td> <td style="text-align:right;"> 1967 </td> <td style="text-align:left;"> … </td> <td style="text-align:right;"> 9648014150 </td> <td style="text-align:right;"> 12 </td> </tr> <tr> <td style="text-align:left;"> Afghanistan </td> <td style="text-align:right;"> 1972 </td> <td style="text-align:left;"> … </td> <td style="text-align:right;"> 9678553274 </td> <td style="text-align:right;"> 13 </td> </tr> <tr> <td style="text-align:left;"> Afghanistan </td> <td style="text-align:right;"> 1977 </td> <td style="text-align:left;"> … </td> <td style="text-align:right;"> 11697659231 </td> <td style="text-align:right;"> 15 </td> </tr> </tbody> </table> ] --- class: title title-1 # `ifelse()` .box-inv-1[Effectuer des tests conditionnels] .pull-left[ ] .pull-right[ - <b><span style="background: #FFC0DC">`TEST`</span></b> = un test logique - <b><span style="background: #FFDFD1">`VALEUR_SI_VRAI`</span></b> = valeur si vrai - <b><span style="background: #CBB5FF">`VALEUR_SI_FAUX`</span></b> = valeur si faux ] .box-inv-1[ Vecteur en sortie de la même longueur que <b>`TEST`</b> ] --- --- ``` ## # A tibble: 1,704 × 7 ## country continent year lifeExp pop gdpPercap after_1960 ## <fct> <fct> <int> <dbl> <int> <dbl> <lgl> ## 1 Afghanistan Asia 1952 28.8 8425333 779. FALSE ## 2 Afghanistan Asia 1957 30.3 9240934 821. FALSE ## 3 Afghanistan Asia 1962 32.0 10267083 853. TRUE ## 4 Afghanistan Asia 1967 34.0 11537966 836. TRUE ## 5 Afghanistan Asia 1972 36.1 13079460 740. TRUE ## 6 Afghanistan Asia 1977 38.4 14880372 786. TRUE ## 7 Afghanistan Asia 1982 39.9 12881816 978. TRUE ## 8 Afghanistan Asia 1987 40.8 13867957 852. TRUE ## 9 Afghanistan Asia 1992 41.7 16317921 649. TRUE ## 10 Afghanistan Asia 1997 41.8 22227415 635. TRUE ## # ℹ 1,694 more rows ``` --- class: title title-1 # `case_when()` .box-inv-1[Tests conditionnels vectorisés] ``` r case_when(CAS1_EST_VRAI ~ VALEUR1, CAS2_EST_VRAI ~ VALEUR2, TRUE ~ VALEUR_AUTRE) ``` .box-inv-1[Énoncés évalués si les précédents ne sont pas vrais.] .box-inv-1[Retourne `NA` pour toute valeur restante si on omet le dernier énoncé] --- ``` r mutate(gapminder, decennie = case_when( year < 1960 ~ "années 50", # year >= 1960 & year < 1970 ~ "années 60", year < 1970 ~ "années 60", year < 1980 ~ "années 70", year < 1990 ~ "années 80", year < 2000 ~ "années 90", TRUE ~ "millénaire") ) ``` --- ``` ## # A tibble: 1,704 × 7 ## country continent year lifeExp pop gdpPercap decennie ## <fct> <fct> <int> <dbl> <int> <dbl> <chr> ## 1 Afghanistan Asia 1952 28.8 8425333 779. années 50 ## 2 Afghanistan Asia 1957 30.3 9240934 821. années 50 ## 3 Afghanistan Asia 1962 32.0 10267083 853. années 60 ## 4 Afghanistan Asia 1967 34.0 11537966 836. années 60 ## 5 Afghanistan Asia 1972 36.1 13079460 740. années 70 ## 6 Afghanistan Asia 1977 38.4 14880372 786. années 70 ## 7 Afghanistan Asia 1982 39.9 12881816 978. années 80 ## 8 Afghanistan Asia 1987 40.8 13867957 852. années 80 ## 9 Afghanistan Asia 1992 41.7 16317921 649. années 90 ## 10 Afghanistan Asia 1997 41.8 22227415 635. années 90 ## # ℹ 1,694 more rows ``` --- class: title title-1 section-title-inv-1 # À votre tour #3: transformer .box-1[Utilisez `mutate()` pour ajouter les colonnes…] 1. `africa`, qui est vrai (`TRUE`) si le pays est situé sur le continent africain 2. `log_gdpPercap` pour le log PIB par capita (indice: utiliser `log()`) 3. `africa_asia` avec comme valeur `"Afrique ou Asie"` si le pays est dans un des deux continents, sinon `"Autre continent"`
--- ``` r mutate(gapminder, africa = continent == "Africa") ``` -- ``` r mutate(gapminder, log_gdpPercap = log(gdpPercap)) ``` -- ``` r mutate(gapminder, africa_asia = ifelse(continent %in% c("Africa", "Asia"), "Afrique ou Asie", "Autre continent")) ``` --- class: title title-1 # Comment procéder avec plusieurs verbes? .box-inv-1.sp-after[Créer un jeu de données pour 2002 *et* calculer le log PIB par capita] -- .box-inv-1[Solution 1: variables auxiliaires] --- class: title title-1 # Comment procéder avec plusieurs verbes? .box-inv-1.sp-after[Créer un jeu de données pour 2002 *et* calculer le log PIB par capita] .box-inv-1[Solution 2: fonctions emboîtées] --- class: title title-1 # Comment procéder avec plusieurs verbes? .box-inv-1.sp-after[Créer un jeu de données pour 2002 *et* calculer le log PIB par capita] .box-inv-1[Solution 3: tuyaux!] .box-inv-1[L'opérateur tuyau, `|>`, prend un objet à gauche<br>et l'assigne au premier argument de la fonction de droite] --- class: title title-1 # Comment procéder avec plusieurs verbes? .box-inv-1[Ces deux lignes donnent le même résultat] --- class: title title-1 # Comment procéder avec plusieurs verbes? .box-inv-1.sp-after[Créer un jeu de données pour 2002 *et* calculer le log PIB par capita] .box-inv-1[Solution 3: Tuyaux!] --- class: title title-1 # Améliorer la lisibilité du code avec `|>` -- --- class: title title-1 # `summarize()` .box-inv-1[Créer un tableau résumé] .pull-left.small[ <table> <thead> <tr> <th style="text-align:left;"> country </th> <th style="text-align:left;"> continent </th> <th style="text-align:left;"> year </th> <th style="text-align:left;"> lifeExp </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> Afghanistan </td> <td style="text-align:left;"> Asia </td> <td style="text-align:left;"> 1952 </td> <td style="text-align:left;"> 28.801 </td> </tr> <tr> <td style="text-align:left;"> Afghanistan </td> <td style="text-align:left;"> Asia </td> <td style="text-align:left;"> 1957 </td> <td style="text-align:left;"> 30.332 </td> </tr> <tr> <td style="text-align:left;"> Afghanistan </td> <td style="text-align:left;"> Asia </td> <td style="text-align:left;"> 1962 </td> <td style="text-align:left;"> 31.997 </td> </tr> <tr> <td style="text-align:left;"> Afghanistan </td> <td style="text-align:left;"> Asia </td> <td style="text-align:left;"> 1967 </td> <td style="text-align:left;"> 34.02 </td> </tr> <tr> <td style="text-align:left;"> … </td> <td style="text-align:left;"> … </td> <td style="text-align:left;"> … </td> <td style="text-align:left;"> … </td> </tr> </tbody> </table> ] -- .pull-right.small[ <table> <thead> <tr> <th style="text-align:right;"> mean_life </th> </tr> </thead> <tbody> <tr> <td style="text-align:right;"> 59.47444 </td> </tr> </tbody> </table> ] --- class: title title-1 # `summarize()` .pull-left.small[ <table> <thead> <tr> <th style="text-align:left;"> country </th> <th style="text-align:left;"> continent </th> <th style="text-align:left;"> year </th> <th style="text-align:left;"> lifeExp </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> Afghanistan </td> <td style="text-align:left;"> Asia </td> <td style="text-align:left;"> 1952 </td> <td style="text-align:left;"> 28.801 </td> </tr> <tr> <td style="text-align:left;"> Afghanistan </td> <td style="text-align:left;"> Asia </td> <td style="text-align:left;"> 1957 </td> <td style="text-align:left;"> 30.332 </td> </tr> <tr> <td style="text-align:left;"> Afghanistan </td> <td style="text-align:left;"> Asia </td> <td style="text-align:left;"> 1962 </td> <td style="text-align:left;"> 31.997 </td> </tr> <tr> <td style="text-align:left;"> Afghanistan </td> <td style="text-align:left;"> Asia </td> <td style="text-align:left;"> 1967 </td> <td style="text-align:left;"> 34.02 </td> </tr> <tr> <td style="text-align:left;"> Afghanistan </td> <td style="text-align:left;"> Asia </td> <td style="text-align:left;"> 1972 </td> <td style="text-align:left;"> 36.088 </td> </tr> <tr> <td style="text-align:left;"> … </td> <td style="text-align:left;"> … </td> <td style="text-align:left;"> … </td> <td style="text-align:left;"> … </td> </tr> </tbody> </table> ] -- .pull-right.small[ <table> <thead> <tr> <th style="text-align:right;"> mean_life </th> <th style="text-align:right;"> min_life </th> </tr> </thead> <tbody> <tr> <td style="text-align:right;"> 59.47444 </td> <td style="text-align:right;"> 23.599 </td> </tr> </tbody> </table> ] --- class: title title-1 section-title-inv-1 # À votre tour #4: résumer .box-1[Utilisez `summarize()` pour calculer…] 1. La première année des mesures (minimum) 2. La dernière année des mesures (maximum) 3. Le nombre de lignes dans la base de données (utilisez l'aide mémoire) 4. Le nombre de pays distincts dans la base de données (utilisez l'aide mémoire)
--- ``` r gapminder |> summarize(first = min(year), last = max(year), num_rows = n(), num_unique = n_distinct(country)) ``` .small[ <table> <thead> <tr> <th style="text-align:right;"> first </th> <th style="text-align:right;"> last </th> <th style="text-align:right;"> num_rows </th> <th style="text-align:right;"> num_unique </th> </tr> </thead> <tbody> <tr> <td style="text-align:right;"> 1952 </td> <td style="text-align:right;"> 2007 </td> <td style="text-align:right;"> 1704 </td> <td style="text-align:right;"> 142 </td> </tr> </tbody> </table> ] --- class: title title-1 section-title-inv-1 # À votre tour #5: résumer .box-1[Utilisez `filter()` et `summarize()` pour calculer] 1. le nombre de pays 2. l'espérance de vie médiane pour le continent africain en 2007.
--- ``` r gapminder |> filter(continent == "Africa", year == 2007) |> summarise(n_countries = n_distinct(country), med_le = median(lifeExp)) ``` .small[ <table> <thead> <tr> <th style="text-align:right;"> n_countries </th> <th style="text-align:right;"> med_le </th> </tr> </thead> <tbody> <tr> <td style="text-align:right;"> 52 </td> <td style="text-align:right;"> 52.9265 </td> </tr> </tbody> </table> ] --- class: title title-1 # `group_by()` .box-inv-1[Assembler les lignes en groupes selon les valeurs d'une colonne] -- .box-inv-1[Rien n'apparaît!] -- .box-inv-1[Outil puissant si combiné avec `summarize()`] --- ``` r gapminder |> group_by(continent) |> summarize(n_countries = n_distinct(country)) ``` -- .small[ <table> <thead> <tr> <th style="text-align:left;"> continent </th> <th style="text-align:right;"> n_countries </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> Africa </td> <td style="text-align:right;"> 52 </td> </tr> <tr> <td style="text-align:left;"> Americas </td> <td style="text-align:right;"> 25 </td> </tr> <tr> <td style="text-align:left;"> Asia </td> <td style="text-align:right;"> 33 </td> </tr> <tr> <td style="text-align:left;"> Europe </td> <td style="text-align:right;"> 30 </td> </tr> <tr> <td style="text-align:left;"> Oceania </td> <td style="text-align:right;"> 2 </td> </tr> </tbody> </table> ] --- .pull-left.small[ <table> <thead> <tr> <th style="text-align:left;"> city </th> <th style="text-align:left;"> particle_size </th> <th style="text-align:right;"> amount </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> New York </td> <td style="text-align:left;"> Large </td> <td style="text-align:right;"> 23 </td> </tr> <tr> <td style="text-align:left;"> New York </td> <td style="text-align:left;"> Small </td> <td style="text-align:right;"> 14 </td> </tr> <tr> <td style="text-align:left;"> London </td> <td style="text-align:left;"> Large </td> <td style="text-align:right;"> 22 </td> </tr> <tr> <td style="text-align:left;"> London </td> <td style="text-align:left;"> Small </td> <td style="text-align:right;"> 16 </td> </tr> <tr> <td style="text-align:left;"> Beijing </td> <td style="text-align:left;"> Large </td> <td style="text-align:right;"> 121 </td> </tr> <tr> <td style="text-align:left;"> Beijing </td> <td style="text-align:left;"> Small </td> <td style="text-align:right;"> 56 </td> </tr> </tbody> </table> ] -- .pull-right.small[ <table> <thead> <tr> <th style="text-align:right;"> mean </th> <th style="text-align:right;"> sum </th> <th style="text-align:right;"> n </th> </tr> </thead> <tbody> <tr> <td style="text-align:right;"> 42 </td> <td style="text-align:right;"> 252 </td> <td style="text-align:right;"> 6 </td> </tr> </tbody> </table> ] --- .pull-left.small[ <table> <thead> <tr> <th style="text-align:left;"> city </th> <th style="text-align:left;"> particle_size </th> <th style="text-align:right;"> amount </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;background-color: rgba(178, 177, 249, 1) !important;"> New York </td> <td style="text-align:left;background-color: rgba(178, 177, 249, 1) !important;"> Large </td> <td style="text-align:right;background-color: rgba(178, 177, 249, 1) !important;"> 23 </td> </tr> <tr> <td style="text-align:left;background-color: rgba(178, 177, 249, 1) !important;"> New York </td> <td style="text-align:left;background-color: rgba(178, 177, 249, 1) !important;"> Small </td> <td style="text-align:right;background-color: rgba(178, 177, 249, 1) !important;"> 14 </td> </tr> <tr> <td style="text-align:left;background-color: rgba(239, 179, 255, 1) !important;"> London </td> <td style="text-align:left;background-color: rgba(239, 179, 255, 1) !important;"> Large </td> <td style="text-align:right;background-color: rgba(239, 179, 255, 1) !important;"> 22 </td> </tr> <tr> <td style="text-align:left;background-color: rgba(239, 179, 255, 1) !important;"> London </td> <td style="text-align:left;background-color: rgba(239, 179, 255, 1) !important;"> Small </td> <td style="text-align:right;background-color: rgba(239, 179, 255, 1) !important;"> 16 </td> </tr> <tr> <td style="text-align:left;background-color: rgba(255, 208, 207, 1) !important;"> Beijing </td> <td style="text-align:left;background-color: rgba(255, 208, 207, 1) !important;"> Large </td> <td style="text-align:right;background-color: rgba(255, 208, 207, 1) !important;"> 121 </td> </tr> <tr> <td style="text-align:left;background-color: rgba(255, 208, 207, 1) !important;"> Beijing </td> <td style="text-align:left;background-color: rgba(255, 208, 207, 1) !important;"> Small </td> <td style="text-align:right;background-color: rgba(255, 208, 207, 1) !important;"> 56 </td> </tr> </tbody> </table> ] -- .pull-right.small[ <table> <thead> <tr> <th style="text-align:left;"> city </th> <th style="text-align:right;"> mean </th> <th style="text-align:right;"> sum </th> <th style="text-align:right;"> n </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;background-color: rgba(255, 208, 207, 1) !important;"> Beijing </td> <td style="text-align:right;background-color: rgba(255, 208, 207, 1) !important;"> 88.5 </td> <td style="text-align:right;background-color: rgba(255, 208, 207, 1) !important;"> 177 </td> <td style="text-align:right;background-color: rgba(255, 208, 207, 1) !important;"> 2 </td> </tr> <tr> <td style="text-align:left;background-color: rgba(239, 179, 255, 1) !important;"> London </td> <td style="text-align:right;background-color: rgba(239, 179, 255, 1) !important;"> 19.0 </td> <td style="text-align:right;background-color: rgba(239, 179, 255, 1) !important;"> 38 </td> <td style="text-align:right;background-color: rgba(239, 179, 255, 1) !important;"> 2 </td> </tr> <tr> <td style="text-align:left;background-color: rgba(178, 177, 249, 1) !important;"> New York </td> <td style="text-align:right;background-color: rgba(178, 177, 249, 1) !important;"> 18.5 </td> <td style="text-align:right;background-color: rgba(178, 177, 249, 1) !important;"> 37 </td> <td style="text-align:right;background-color: rgba(178, 177, 249, 1) !important;"> 2 </td> </tr> </tbody> </table> ] --- .pull-left.small[ <table> <thead> <tr> <th style="text-align:left;"> city </th> <th style="text-align:left;"> particle_size </th> <th style="text-align:right;"> amount </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;background-color: rgba(255, 223, 209, 1) !important;"> New York </td> <td style="text-align:left;background-color: rgba(255, 223, 209, 1) !important;"> Large </td> <td style="text-align:right;background-color: rgba(255, 223, 209, 1) !important;"> 23 </td> </tr> <tr> <td style="text-align:left;background-color: rgba(255, 240, 212, 1) !important;"> New York </td> <td style="text-align:left;background-color: rgba(255, 240, 212, 1) !important;"> Small </td> <td style="text-align:right;background-color: rgba(255, 240, 212, 1) !important;"> 14 </td> </tr> <tr> <td style="text-align:left;background-color: rgba(255, 223, 209, 1) !important;"> London </td> <td style="text-align:left;background-color: rgba(255, 223, 209, 1) !important;"> Large </td> <td style="text-align:right;background-color: rgba(255, 223, 209, 1) !important;"> 22 </td> </tr> <tr> <td style="text-align:left;background-color: rgba(255, 240, 212, 1) !important;"> London </td> <td style="text-align:left;background-color: rgba(255, 240, 212, 1) !important;"> Small </td> <td style="text-align:right;background-color: rgba(255, 240, 212, 1) !important;"> 16 </td> </tr> <tr> <td style="text-align:left;background-color: rgba(255, 223, 209, 1) !important;"> Beijing </td> <td style="text-align:left;background-color: rgba(255, 223, 209, 1) !important;"> Large </td> <td style="text-align:right;background-color: rgba(255, 223, 209, 1) !important;"> 121 </td> </tr> <tr> <td style="text-align:left;background-color: rgba(255, 240, 212, 1) !important;"> Beijing </td> <td style="text-align:left;background-color: rgba(255, 240, 212, 1) !important;"> Small </td> <td style="text-align:right;background-color: rgba(255, 240, 212, 1) !important;"> 56 </td> </tr> </tbody> </table> ] -- .pull-right.small[ <table> <thead> <tr> <th style="text-align:left;"> particle_size </th> <th style="text-align:right;"> mean </th> <th style="text-align:right;"> sum </th> <th style="text-align:right;"> n </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;background-color: rgba(255, 223, 209, 1) !important;"> Large </td> <td style="text-align:right;background-color: rgba(255, 223, 209, 1) !important;"> 55.33333 </td> <td style="text-align:right;background-color: rgba(255, 223, 209, 1) !important;"> 166 </td> <td style="text-align:right;background-color: rgba(255, 223, 209, 1) !important;"> 3 </td> </tr> <tr> <td style="text-align:left;background-color: rgba(255, 240, 212, 1) !important;"> Small </td> <td style="text-align:right;background-color: rgba(255, 240, 212, 1) !important;"> 28.66667 </td> <td style="text-align:right;background-color: rgba(255, 240, 212, 1) !important;"> 86 </td> <td style="text-align:right;background-color: rgba(255, 240, 212, 1) !important;"> 3 </td> </tr> </tbody> </table> ] --- class: title title-1 section-title-inv-1 # À votre tour #6: grouper et résumer .box-1[Trouvez l'espérance de vie <br>minimum, maximum et médiane<br>par continent] .box-1[Trouvez l'espérance de vie <br>minimum, maximum et médiane<br>par continent pour 2007 uniquement]
--- ``` r gapminder |> group_by(continent) |> summarize(min_le = min(lifeExp), max_le = max(lifeExp), med_le = median(lifeExp)) ``` -- ``` r gapminder |> filter(year == 2007) |> group_by(continent) |> summarize(min_le = min(lifeExp), max_le = max(lifeExp), med_le = median(lifeExp)) ``` --- class: title title-1 # `dplyr`: verbes pour manipuler des données <table> <tr> <td>Extraire des lignes avec <code>filter()</code></td> <td><img src="img/04/filter.png" alt="filter" title="filter" height="80px"></td> </tr> <tr> <td>Extraire des colonnes avec <code>select()</code></td> <td><img src="img/04/select.png" alt="select" title="select" height="80px"></td> </tr> <tr> <td>Arranger/trier les lignes avec <code>arrange()</code></td> <td><img src="img/04/arrange.png" alt="arrange" title="arrange" height="80px"></td> </tr> <tr> <td>Créer/modifier des colonnes avec <code>mutate()</code></td> <td><img src="img/04/mutate.png" alt="mutate" title="mutate" height="80px"></td> </tr> <tr> <td>Résumer des sous-ensembles avec<br><code>group_by() |> summarize()</code></td> <td><img src="img/04/summarize.png" alt="summarize" title="summarize" height="80px"></td> </tr> </table>