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 ``` ``` ## country continent year lifeExp pop gdpPercap ## 1 Afghanistan Asia 1952 28.801 8425333 779.4453 ## 2 Afghanistan Asia 1957 30.332 9240934 820.8530 ## 3 Afghanistan Asia 1962 31.997 10267083 853.1007 ## 4 Afghanistan Asia 1967 34.020 11537966 836.1971 ## 5 Afghanistan Asia 1972 36.088 13079460 739.9811 ## 6 Afghanistan Asia 1977 38.438 14880372 786.1134 ## 7 Afghanistan Asia 1982 39.854 12881816 978.0114 ## 8 Afghanistan Asia 1987 40.822 13867957 852.3959 ## 9 Afghanistan Asia 1992 41.674 16317921 649.3414 ## 10 Afghanistan Asia 1997 41.763 22227415 635.3414 ## 11 Afghanistan Asia 2002 42.129 25268405 726.7341 ## 12 Afghanistan Asia 2007 43.828 31889923 974.5803 ## 13 Albania Europe 1952 55.230 1282697 1601.0561 ## 14 Albania Europe 1957 59.280 1476505 1942.2842 ## 15 Albania Europe 1962 64.820 1728137 2312.8890 ## 16 Albania Europe 1967 66.220 1984060 2760.1969 ## 17 Albania Europe 1972 67.690 2263554 3313.4222 ## 18 Albania Europe 1977 68.930 2509048 3533.0039 ## 19 Albania Europe 1982 70.420 2780097 3630.8807 ## 20 Albania Europe 1987 72.000 3075321 3738.9327 ## 21 Albania Europe 1992 71.581 3326498 2497.4379 ## 22 Albania Europe 1997 72.950 3428038 3193.0546 ## 23 Albania Europe 2002 75.651 3508512 4604.2117 ## 24 Albania Europe 2007 76.423 3600523 5937.0295 ## 25 Algeria Africa 1952 43.077 9279525 2449.0082 ## 26 Algeria Africa 1957 45.685 10270856 3013.9760 ## 27 Algeria Africa 1962 48.303 11000948 2550.8169 ## 28 Algeria Africa 1967 51.407 12760499 3246.9918 ## 29 Algeria Africa 1972 54.518 14760787 4182.6638 ## 30 Algeria Africa 1977 58.014 17152804 4910.4168 ## 31 Algeria Africa 1982 61.368 20033753 5745.1602 ## 32 Algeria Africa 1987 65.799 23254956 5681.3585 ## 33 Algeria Africa 1992 67.744 26298373 5023.2166 ## 34 Algeria Africa 1997 69.152 29072015 4797.2951 ## 35 Algeria Africa 2002 70.994 31287142 5288.0404 ## 36 Algeria Africa 2007 72.301 33333216 6223.3675 ## 37 Angola Africa 1952 30.015 4232095 3520.6103 ## 38 Angola Africa 1957 31.999 4561361 3827.9405 ## 39 Angola Africa 1962 34.000 4826015 4269.2767 ## 40 Angola Africa 1967 35.985 5247469 5522.7764 ## 41 Angola Africa 1972 37.928 5894858 5473.2880 ## 42 Angola Africa 1977 39.483 6162675 3008.6474 ## 43 Angola Africa 1982 39.942 7016384 2756.9537 ## 44 Angola Africa 1987 39.906 7874230 2430.2083 ## 45 Angola Africa 1992 40.647 8735988 2627.8457 ## 46 Angola Africa 1997 40.963 9875024 2277.1409 ## 47 Angola Africa 2002 41.003 10866106 2773.2873 ## 48 Angola Africa 2007 42.731 12420476 4797.2313 ## 49 Argentina Americas 1952 62.485 17876956 5911.3151 ## 50 Argentina Americas 1957 64.399 19610538 6856.8562 ## 51 Argentina Americas 1962 65.142 21283783 7133.1660 ## 52 Argentina Americas 1967 65.634 22934225 8052.9530 ## 53 Argentina Americas 1972 67.065 24779799 9443.0385 ## 54 Argentina Americas 1977 68.481 26983828 10079.0267 ## 55 Argentina Americas 1982 69.942 29341374 8997.8974 ## 56 Argentina Americas 1987 70.774 31620918 9139.6714 ## 57 Argentina Americas 1992 71.868 33958947 9308.4187 ## 58 Argentina Americas 1997 73.275 36203463 10967.2820 ## 59 Argentina Americas 2002 74.340 38331121 8797.6407 ## 60 Argentina Americas 2007 75.320 40301927 12779.3796 ## 61 Australia Oceania 1952 69.120 8691212 10039.5956 ## 62 Australia Oceania 1957 70.330 9712569 10949.6496 ## 63 Australia Oceania 1962 70.930 10794968 12217.2269 ## 64 Australia Oceania 1967 71.100 11872264 14526.1246 ## 65 Australia Oceania 1972 71.930 13177000 16788.6295 ## 66 Australia Oceania 1977 73.490 14074100 18334.1975 ## 67 Australia Oceania 1982 74.740 15184200 19477.0093 ## 68 Australia Oceania 1987 76.320 16257249 21888.8890 ## 69 Australia Oceania 1992 77.560 17481977 23424.7668 ## 70 Australia Oceania 1997 78.830 18565243 26997.9366 ## 71 Australia Oceania 2002 80.370 19546792 30687.7547 ## 72 Australia Oceania 2007 81.235 20434176 34435.3674 ## 73 Austria Europe 1952 66.800 6927772 6137.0765 ## 74 Austria Europe 1957 67.480 6965860 8842.5980 ## 75 Austria Europe 1962 69.540 7129864 10750.7211 ## 76 Austria Europe 1967 70.140 7376998 12834.6024 ## 77 Austria Europe 1972 70.630 7544201 16661.6256 ## 78 Austria Europe 1977 72.170 7568430 19749.4223 ## 79 Austria Europe 1982 73.180 7574613 21597.0836 ## 80 Austria Europe 1987 74.940 7578903 23687.8261 ## 81 Austria Europe 1992 76.040 7914969 27042.0187 ## 82 Austria Europe 1997 77.510 8069876 29095.9207 ## 83 Austria Europe 2002 78.980 8148312 32417.6077 ## 84 Austria Europe 2007 79.829 8199783 36126.4927 ## 85 Bahrain Asia 1952 50.939 120447 9867.0848 ## 86 Bahrain Asia 1957 53.832 138655 11635.7995 ## 87 Bahrain Asia 1962 56.923 171863 12753.2751 ## 88 Bahrain Asia 1967 59.923 202182 14804.6727 ## 89 Bahrain Asia 1972 63.300 230800 18268.6584 ## 90 Bahrain Asia 1977 65.593 297410 19340.1020 ## 91 Bahrain Asia 1982 69.052 377967 19211.1473 ## 92 Bahrain Asia 1987 70.750 454612 18524.0241 ## 93 Bahrain Asia 1992 72.601 529491 19035.5792 ## 94 Bahrain Asia 1997 73.925 598561 20292.0168 ## 95 Bahrain Asia 2002 74.795 656397 23403.5593 ## 96 Bahrain Asia 2007 75.635 708573 29796.0483 ## 97 Bangladesh Asia 1952 37.484 46886859 684.2442 ## 98 Bangladesh Asia 1957 39.348 51365468 661.6375 ## 99 Bangladesh Asia 1962 41.216 56839289 686.3416 ## 100 Bangladesh Asia 1967 43.453 62821884 721.1861 ## 101 Bangladesh Asia 1972 45.252 70759295 630.2336 ## 102 Bangladesh Asia 1977 46.923 80428306 659.8772 ## 103 Bangladesh Asia 1982 50.009 93074406 676.9819 ## 104 Bangladesh Asia 1987 52.819 103764241 751.9794 ## 105 Bangladesh Asia 1992 56.018 113704579 837.8102 ## 106 Bangladesh Asia 1997 59.412 123315288 972.7700 ## 107 Bangladesh Asia 2002 62.013 135656790 1136.3904 ## 108 Bangladesh Asia 2007 64.062 150448339 1391.2538 ## 109 Belgium Europe 1952 68.000 8730405 8343.1051 ## 110 Belgium Europe 1957 69.240 8989111 9714.9606 ## 111 Belgium Europe 1962 70.250 9218400 10991.2068 ## 112 Belgium Europe 1967 70.940 9556500 13149.0412 ## 113 Belgium Europe 1972 71.440 9709100 16672.1436 ## 114 Belgium Europe 1977 72.800 9821800 19117.9745 ## 115 Belgium Europe 1982 73.930 9856303 20979.8459 ## 116 Belgium Europe 1987 75.350 9870200 22525.5631 ## 117 Belgium Europe 1992 76.460 10045622 25575.5707 ## 118 Belgium Europe 1997 77.530 10199787 27561.1966 ## 119 Belgium Europe 2002 78.320 10311970 30485.8838 ## 120 Belgium Europe 2007 79.441 10392226 33692.6051 ## 121 Benin Africa 1952 38.223 1738315 1062.7522 ## 122 Benin Africa 1957 40.358 1925173 959.6011 ## 123 Benin Africa 1962 42.618 2151895 949.4991 ## 124 Benin Africa 1967 44.885 2427334 1035.8314 ## 125 Benin Africa 1972 47.014 2761407 1085.7969 ## 126 Benin Africa 1977 49.190 3168267 1029.1613 ## 127 Benin Africa 1982 50.904 3641603 1277.8976 ## 128 Benin Africa 1987 52.337 4243788 1225.8560 ## 129 Benin Africa 1992 53.919 4981671 1191.2077 ## 130 Benin Africa 1997 54.777 6066080 1232.9753 ## 131 Benin Africa 2002 54.406 7026113 1372.8779 ## 132 Benin Africa 2007 56.728 8078314 1441.2849 ## 133 Bolivia Americas 1952 40.414 2883315 2677.3263 ## 134 Bolivia Americas 1957 41.890 3211738 2127.6863 ## 135 Bolivia Americas 1962 43.428 3593918 2180.9725 ## 136 Bolivia Americas 1967 45.032 4040665 2586.8861 ## 137 Bolivia Americas 1972 46.714 4565872 2980.3313 ## 138 Bolivia Americas 1977 50.023 5079716 3548.0978 ## 139 Bolivia Americas 1982 53.859 5642224 3156.5105 ## 140 Bolivia Americas 1987 57.251 6156369 2753.6915 ## 141 Bolivia Americas 1992 59.957 6893451 2961.6997 ## 142 Bolivia Americas 1997 62.050 7693188 3326.1432 ## 143 Bolivia Americas 2002 63.883 8445134 3413.2627 ## 144 Bolivia Americas 2007 65.554 9119152 3822.1371 ## 145 Bosnia and Herzegovina Europe 1952 53.820 2791000 973.5332 ## 146 Bosnia and Herzegovina Europe 1957 58.450 3076000 1353.9892 ## 147 Bosnia and Herzegovina Europe 1962 61.930 3349000 1709.6837 ## 148 Bosnia and Herzegovina Europe 1967 64.790 3585000 2172.3524 ## 149 Bosnia and Herzegovina Europe 1972 67.450 3819000 2860.1698 ## 150 Bosnia and Herzegovina Europe 1977 69.860 4086000 3528.4813 ## 151 Bosnia and Herzegovina Europe 1982 70.690 4172693 4126.6132 ## 152 Bosnia and Herzegovina Europe 1987 71.140 4338977 4314.1148 ## 153 Bosnia and Herzegovina Europe 1992 72.178 4256013 2546.7814 ## 154 Bosnia and Herzegovina Europe 1997 73.244 3607000 4766.3559 ## 155 Bosnia and Herzegovina Europe 2002 74.090 4165416 6018.9752 ## 156 Bosnia and Herzegovina Europe 2007 74.852 4552198 7446.2988 ## 157 Botswana Africa 1952 47.622 442308 851.2411 ## 158 Botswana Africa 1957 49.618 474639 918.2325 ## 159 Botswana Africa 1962 51.520 512764 983.6540 ## 160 Botswana Africa 1967 53.298 553541 1214.7093 ## 161 Botswana Africa 1972 56.024 619351 2263.6111 ## 162 Botswana Africa 1977 59.319 781472 3214.8578 ## 163 Botswana Africa 1982 61.484 970347 4551.1421 ## 164 Botswana Africa 1987 63.622 1151184 6205.8839 ## 165 Botswana Africa 1992 62.745 1342614 7954.1116 ## 166 Botswana Africa 1997 52.556 1536536 8647.1423 ## [ reached 'max' / getOption("max.print") -- omitted 1538 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[ <code class ='r hljs remark-code'>filter(.data = <b><span style="background-color:#FFDFD1">DATA</span></b>, <b><span style="background-color:#FFD0CF">...</span></b>)</code> ] .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`] ] --- <code class ='r hljs remark-code'>filter(.data = <b><span style="background-color:#FFDFD1">gapminder</span></b>, <b><span style="background-color:#FFD0CF">country == "Denmark"</span></b>)</code> .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[ <code class ='r hljs remark-code'>filter(.data = <b><span style="background-color:#FFDFD1">gapminder</span></b>, <br> <b><span style="background-color:#FFD0CF">country == "Denmark"</span></b>)</code> ] .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
−
+
03
:
00
--- .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 `==`] <code class ='r hljs remark-code'>filter(gapminder, <br> country <b><span style="color:#FF4136">=</span></b> "Canada")</code> <code class ='r hljs remark-code'>filter(gapminder, <br> country <b><span style="color:#2ECC40">==</span></b> "Canada")</code> ] -- .pull-right[ .box-inv-1[Mettre entre accolade] <code class ='r hljs remark-code'>filter(gapminder, <br> country == <b><span style="color:#FF4136">Canada</span></b>)</code> <code class ='r hljs remark-code'>filter(gapminder, <br> country == <b><span style="color:#2ECC40">"Canada"</span></b>)</code> ] --- class: title title-1 # `filter()` avec des conditions multiples .box-inv-1[Extrait les lignes qui valident *tous* les énoncés] <code class ='r hljs remark-code'>filter(<b><span style="background-color:#FFDFD1">gapminder</span></b>, <b><span style="background-color:#FFD0CF">country == "Denmark", year > 2000</span></b>)</code> --- <code class ='r hljs remark-code'>filter(<b><span style="background-color:#FFDFD1">gapminder</span></b>, <b><span style="background-color:#FFD0CF">country == "Denmark", year > 2000</span></b>)</code> .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] <code class ='r hljs remark-code'>filter(<b><span style="background-color:#FFDFD1">gapminder</span></b>, <b><span style="background-color:#FFD0CF">country == "Denmark", year > 2000</span></b>)</code> <code class ='r hljs remark-code'>filter(<b><span style="background-color:#FFDFD1">gapminder</span></b>, <b><span style="background-color:#FFD0CF">country == "Denmark" & year > 2000</span></b>)</code> --- 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
−
+
04
:
00
--- ```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[ <code class ='r hljs remark-code'>filter(gapminder, <b><span style="color:#FF4136">1960 < year < 1980</span></b>)</code> ] .small-code[ <code class ='r hljs remark-code'>filter(gapminder, <br> <b><span style="color:#2ECC40">year > 1960, year < 1980</span></b>)</code> ] ] -- .pull-right[ .box-inv-1[Utiliser plusieurs tests<br> plutôt que `%in%`] .small-code[ <code class ='r hljs remark-code'>filter(gapminder, <br> <b><span style="color:#FF4136">country == "Mexico", <br> country == "Canada", <br> country == "United States"</span></b>)</code> ] .small-code[ <code class ='r hljs remark-code'>filter(gapminder, <br> <b><span style="color:#2ECC40">country %in% c("Mexico", "Canada", <br> "United States")</span></b>)</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[ <code class ='r hljs remark-code'><b><span style="background-color:#EFB3FF">VERB</span></b>(<b><span style="background-color:#FFDFD1">DATA</span></b>, <b><span style="background-color:#FFD0CF">...</span></b>)</code> ] .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[ <code class ='r hljs remark-code'>mutate(<b><span style="background-color:#FFDFD1">.data</span></b>, <b><span style="background-color:#FFD0CF">...</span></b>)</code> ] .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 ] --- <code class ='r hljs remark-code'>mutate(<b><span style="background-color:#FFDFD1">gapminder</span></b>, <b><span style="background-color:#FFD0CF">gdp = gdpPercap * pop</span></b>)</code> .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> ] --- <code class ='r hljs remark-code'>mutate(<b><span style="background-color:#FFDFD1">gapminder</span></b>, <b><span style="background-color:#FFD0CF">gdp = gdpPercap * pop,</span></b><br> <b><span style="background-color:#FFD0CF">pop_mil = round(pop / 1000000)</span></b>)</code> .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[ <code class ='r hljs remark-code'>ifelse(<b><span style="background-color:#FFC0DC">TEST</span></b>, <br> <b><span style="background-color:#FFDFD1">VALEUR_SI_VRAI</span></b>, <br> <b><span style="background-color:#CBB5FF">VALEUR_SI_FAUX</span></b>)</code> ] .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> ] --- <code class ='r hljs remark-code'>mutate(gapminder, <br> after_1960 = <b><span style="background-color:#FFC0DC">year > 1960</span></b>)</code> <code class ='r hljs remark-code'>mutate(gapminder, <br> after_1960 = ifelse(<b><span style="background-color:#FFC0DC">year > 1960</span></b>, <br> <b><span style="background-color:#FFDFD1">"After 1960"</span></b>, <br> <b><span style="background-color:#CBB5FF">"Before 1960"</span></b>))</code> --- <code class ='r hljs remark-code'>mutate(gapminder, <br> after_1960 = <b><span style="background-color:#FFC0DC">year > 1960</span></b>)</code> ``` ## country continent year lifeExp pop gdpPercap after_1960 ## 1 Afghanistan Asia 1952 28.801 8425333 779.4453 FALSE ## 2 Afghanistan Asia 1957 30.332 9240934 820.8530 FALSE ## 3 Afghanistan Asia 1962 31.997 10267083 853.1007 TRUE ## 4 Afghanistan Asia 1967 34.020 11537966 836.1971 TRUE ## 5 Afghanistan Asia 1972 36.088 13079460 739.9811 TRUE ## 6 Afghanistan Asia 1977 38.438 14880372 786.1134 TRUE ## 7 Afghanistan Asia 1982 39.854 12881816 978.0114 TRUE ## 8 Afghanistan Asia 1987 40.822 13867957 852.3959 TRUE ## 9 Afghanistan Asia 1992 41.674 16317921 649.3414 TRUE ## 10 Afghanistan Asia 1997 41.763 22227415 635.3414 TRUE ## 11 Afghanistan Asia 2002 42.129 25268405 726.7341 TRUE ## 12 Afghanistan Asia 2007 43.828 31889923 974.5803 TRUE ## 13 Albania Europe 1952 55.230 1282697 1601.0561 FALSE ## 14 Albania Europe 1957 59.280 1476505 1942.2842 FALSE ## 15 Albania Europe 1962 64.820 1728137 2312.8890 TRUE ## 16 Albania Europe 1967 66.220 1984060 2760.1969 TRUE ## 17 Albania Europe 1972 67.690 2263554 3313.4222 TRUE ## 18 Albania Europe 1977 68.930 2509048 3533.0039 TRUE ## 19 Albania Europe 1982 70.420 2780097 3630.8807 TRUE ## 20 Albania Europe 1987 72.000 3075321 3738.9327 TRUE ## 21 Albania Europe 1992 71.581 3326498 2497.4379 TRUE ## 22 Albania Europe 1997 72.950 3428038 3193.0546 TRUE ## 23 Albania Europe 2002 75.651 3508512 4604.2117 TRUE ## 24 Albania Europe 2007 76.423 3600523 5937.0295 TRUE ## 25 Algeria Africa 1952 43.077 9279525 2449.0082 FALSE ## 26 Algeria Africa 1957 45.685 10270856 3013.9760 FALSE ## 27 Algeria Africa 1962 48.303 11000948 2550.8169 TRUE ## 28 Algeria Africa 1967 51.407 12760499 3246.9918 TRUE ## 29 Algeria Africa 1972 54.518 14760787 4182.6638 TRUE ## 30 Algeria Africa 1977 58.014 17152804 4910.4168 TRUE ## 31 Algeria Africa 1982 61.368 20033753 5745.1602 TRUE ## 32 Algeria Africa 1987 65.799 23254956 5681.3585 TRUE ## 33 Algeria Africa 1992 67.744 26298373 5023.2166 TRUE ## 34 Algeria Africa 1997 69.152 29072015 4797.2951 TRUE ## 35 Algeria Africa 2002 70.994 31287142 5288.0404 TRUE ## 36 Algeria Africa 2007 72.301 33333216 6223.3675 TRUE ## 37 Angola Africa 1952 30.015 4232095 3520.6103 FALSE ## 38 Angola Africa 1957 31.999 4561361 3827.9405 FALSE ## 39 Angola Africa 1962 34.000 4826015 4269.2767 TRUE ## 40 Angola Africa 1967 35.985 5247469 5522.7764 TRUE ## 41 Angola Africa 1972 37.928 5894858 5473.2880 TRUE ## 42 Angola Africa 1977 39.483 6162675 3008.6474 TRUE ## 43 Angola Africa 1982 39.942 7016384 2756.9537 TRUE ## 44 Angola Africa 1987 39.906 7874230 2430.2083 TRUE ## 45 Angola Africa 1992 40.647 8735988 2627.8457 TRUE ## 46 Angola Africa 1997 40.963 9875024 2277.1409 TRUE ## 47 Angola Africa 2002 41.003 10866106 2773.2873 TRUE ## 48 Angola Africa 2007 42.731 12420476 4797.2313 TRUE ## 49 Argentina Americas 1952 62.485 17876956 5911.3151 FALSE ## 50 Argentina Americas 1957 64.399 19610538 6856.8562 FALSE ## 51 Argentina Americas 1962 65.142 21283783 7133.1660 TRUE ## 52 Argentina Americas 1967 65.634 22934225 8052.9530 TRUE ## 53 Argentina Americas 1972 67.065 24779799 9443.0385 TRUE ## 54 Argentina Americas 1977 68.481 26983828 10079.0267 TRUE ## 55 Argentina Americas 1982 69.942 29341374 8997.8974 TRUE ## 56 Argentina Americas 1987 70.774 31620918 9139.6714 TRUE ## 57 Argentina Americas 1992 71.868 33958947 9308.4187 TRUE ## 58 Argentina Americas 1997 73.275 36203463 10967.2820 TRUE ## 59 Argentina Americas 2002 74.340 38331121 8797.6407 TRUE ## 60 Argentina Americas 2007 75.320 40301927 12779.3796 TRUE ## 61 Australia Oceania 1952 69.120 8691212 10039.5956 FALSE ## 62 Australia Oceania 1957 70.330 9712569 10949.6496 FALSE ## 63 Australia Oceania 1962 70.930 10794968 12217.2269 TRUE ## 64 Australia Oceania 1967 71.100 11872264 14526.1246 TRUE ## 65 Australia Oceania 1972 71.930 13177000 16788.6295 TRUE ## 66 Australia Oceania 1977 73.490 14074100 18334.1975 TRUE ## 67 Australia Oceania 1982 74.740 15184200 19477.0093 TRUE ## 68 Australia Oceania 1987 76.320 16257249 21888.8890 TRUE ## 69 Australia Oceania 1992 77.560 17481977 23424.7668 TRUE ## 70 Australia Oceania 1997 78.830 18565243 26997.9366 TRUE ## 71 Australia Oceania 2002 80.370 19546792 30687.7547 TRUE ## 72 Australia Oceania 2007 81.235 20434176 34435.3674 TRUE ## 73 Austria Europe 1952 66.800 6927772 6137.0765 FALSE ## 74 Austria Europe 1957 67.480 6965860 8842.5980 FALSE ## 75 Austria Europe 1962 69.540 7129864 10750.7211 TRUE ## 76 Austria Europe 1967 70.140 7376998 12834.6024 TRUE ## 77 Austria Europe 1972 70.630 7544201 16661.6256 TRUE ## 78 Austria Europe 1977 72.170 7568430 19749.4223 TRUE ## 79 Austria Europe 1982 73.180 7574613 21597.0836 TRUE ## 80 Austria Europe 1987 74.940 7578903 23687.8261 TRUE ## 81 Austria Europe 1992 76.040 7914969 27042.0187 TRUE ## 82 Austria Europe 1997 77.510 8069876 29095.9207 TRUE ## 83 Austria Europe 2002 78.980 8148312 32417.6077 TRUE ## 84 Austria Europe 2007 79.829 8199783 36126.4927 TRUE ## 85 Bahrain Asia 1952 50.939 120447 9867.0848 FALSE ## 86 Bahrain Asia 1957 53.832 138655 11635.7995 FALSE ## 87 Bahrain Asia 1962 56.923 171863 12753.2751 TRUE ## 88 Bahrain Asia 1967 59.923 202182 14804.6727 TRUE ## 89 Bahrain Asia 1972 63.300 230800 18268.6584 TRUE ## 90 Bahrain Asia 1977 65.593 297410 19340.1020 TRUE ## 91 Bahrain Asia 1982 69.052 377967 19211.1473 TRUE ## 92 Bahrain Asia 1987 70.750 454612 18524.0241 TRUE ## 93 Bahrain Asia 1992 72.601 529491 19035.5792 TRUE ## 94 Bahrain Asia 1997 73.925 598561 20292.0168 TRUE ## 95 Bahrain Asia 2002 74.795 656397 23403.5593 TRUE ## 96 Bahrain Asia 2007 75.635 708573 29796.0483 TRUE ## 97 Bangladesh Asia 1952 37.484 46886859 684.2442 FALSE ## 98 Bangladesh Asia 1957 39.348 51365468 661.6375 FALSE ## 99 Bangladesh Asia 1962 41.216 56839289 686.3416 TRUE ## 100 Bangladesh Asia 1967 43.453 62821884 721.1861 TRUE ## 101 Bangladesh Asia 1972 45.252 70759295 630.2336 TRUE ## 102 Bangladesh Asia 1977 46.923 80428306 659.8772 TRUE ## 103 Bangladesh Asia 1982 50.009 93074406 676.9819 TRUE ## 104 Bangladesh Asia 1987 52.819 103764241 751.9794 TRUE ## 105 Bangladesh Asia 1992 56.018 113704579 837.8102 TRUE ## 106 Bangladesh Asia 1997 59.412 123315288 972.7700 TRUE ## 107 Bangladesh Asia 2002 62.013 135656790 1136.3904 TRUE ## 108 Bangladesh Asia 2007 64.062 150448339 1391.2538 TRUE ## 109 Belgium Europe 1952 68.000 8730405 8343.1051 FALSE ## 110 Belgium Europe 1957 69.240 8989111 9714.9606 FALSE ## 111 Belgium Europe 1962 70.250 9218400 10991.2068 TRUE ## 112 Belgium Europe 1967 70.940 9556500 13149.0412 TRUE ## 113 Belgium Europe 1972 71.440 9709100 16672.1436 TRUE ## 114 Belgium Europe 1977 72.800 9821800 19117.9745 TRUE ## 115 Belgium Europe 1982 73.930 9856303 20979.8459 TRUE ## 116 Belgium Europe 1987 75.350 9870200 22525.5631 TRUE ## 117 Belgium Europe 1992 76.460 10045622 25575.5707 TRUE ## 118 Belgium Europe 1997 77.530 10199787 27561.1966 TRUE ## 119 Belgium Europe 2002 78.320 10311970 30485.8838 TRUE ## 120 Belgium Europe 2007 79.441 10392226 33692.6051 TRUE ## 121 Benin Africa 1952 38.223 1738315 1062.7522 FALSE ## 122 Benin Africa 1957 40.358 1925173 959.6011 FALSE ## 123 Benin Africa 1962 42.618 2151895 949.4991 TRUE ## 124 Benin Africa 1967 44.885 2427334 1035.8314 TRUE ## 125 Benin Africa 1972 47.014 2761407 1085.7969 TRUE ## 126 Benin Africa 1977 49.190 3168267 1029.1613 TRUE ## 127 Benin Africa 1982 50.904 3641603 1277.8976 TRUE ## 128 Benin Africa 1987 52.337 4243788 1225.8560 TRUE ## 129 Benin Africa 1992 53.919 4981671 1191.2077 TRUE ## 130 Benin Africa 1997 54.777 6066080 1232.9753 TRUE ## 131 Benin Africa 2002 54.406 7026113 1372.8779 TRUE ## 132 Benin Africa 2007 56.728 8078314 1441.2849 TRUE ## 133 Bolivia Americas 1952 40.414 2883315 2677.3263 FALSE ## 134 Bolivia Americas 1957 41.890 3211738 2127.6863 FALSE ## 135 Bolivia Americas 1962 43.428 3593918 2180.9725 TRUE ## 136 Bolivia Americas 1967 45.032 4040665 2586.8861 TRUE ## 137 Bolivia Americas 1972 46.714 4565872 2980.3313 TRUE ## 138 Bolivia Americas 1977 50.023 5079716 3548.0978 TRUE ## 139 Bolivia Americas 1982 53.859 5642224 3156.5105 TRUE ## 140 Bolivia Americas 1987 57.251 6156369 2753.6915 TRUE ## 141 Bolivia Americas 1992 59.957 6893451 2961.6997 TRUE ## 142 Bolivia Americas 1997 62.050 7693188 3326.1432 TRUE ## [ reached 'max' / getOption("max.print") -- omitted 1562 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 & year < 1980 ~ "années 70", year >= 1980 & year < 1990 ~ "années 80", year >= 1990 & year < 2000 ~ "années 90", TRUE ~ "millénaire") ) ``` --- ``` ## country continent year lifeExp pop gdpPercap decennie ## 1 Afghanistan Asia 1952 28.801 8425333 779.4453 années 50 ## 2 Afghanistan Asia 1957 30.332 9240934 820.8530 années 50 ## 3 Afghanistan Asia 1962 31.997 10267083 853.1007 années 60 ## 4 Afghanistan Asia 1967 34.020 11537966 836.1971 années 60 ## 5 Afghanistan Asia 1972 36.088 13079460 739.9811 années 70 ## 6 Afghanistan Asia 1977 38.438 14880372 786.1134 années 70 ## 7 Afghanistan Asia 1982 39.854 12881816 978.0114 années 80 ## 8 Afghanistan Asia 1987 40.822 13867957 852.3959 années 80 ## 9 Afghanistan Asia 1992 41.674 16317921 649.3414 années 90 ## 10 Afghanistan Asia 1997 41.763 22227415 635.3414 années 90 ## 11 Afghanistan Asia 2002 42.129 25268405 726.7341 millénaire ## 12 Afghanistan Asia 2007 43.828 31889923 974.5803 millénaire ## 13 Albania Europe 1952 55.230 1282697 1601.0561 années 50 ## 14 Albania Europe 1957 59.280 1476505 1942.2842 années 50 ## 15 Albania Europe 1962 64.820 1728137 2312.8890 années 60 ## 16 Albania Europe 1967 66.220 1984060 2760.1969 années 60 ## 17 Albania Europe 1972 67.690 2263554 3313.4222 années 70 ## 18 Albania Europe 1977 68.930 2509048 3533.0039 années 70 ## 19 Albania Europe 1982 70.420 2780097 3630.8807 années 80 ## 20 Albania Europe 1987 72.000 3075321 3738.9327 années 80 ## 21 Albania Europe 1992 71.581 3326498 2497.4379 années 90 ## 22 Albania Europe 1997 72.950 3428038 3193.0546 années 90 ## 23 Albania Europe 2002 75.651 3508512 4604.2117 millénaire ## 24 Albania Europe 2007 76.423 3600523 5937.0295 millénaire ## 25 Algeria Africa 1952 43.077 9279525 2449.0082 années 50 ## 26 Algeria Africa 1957 45.685 10270856 3013.9760 années 50 ## 27 Algeria Africa 1962 48.303 11000948 2550.8169 années 60 ## 28 Algeria Africa 1967 51.407 12760499 3246.9918 années 60 ## 29 Algeria Africa 1972 54.518 14760787 4182.6638 années 70 ## 30 Algeria Africa 1977 58.014 17152804 4910.4168 années 70 ## 31 Algeria Africa 1982 61.368 20033753 5745.1602 années 80 ## 32 Algeria Africa 1987 65.799 23254956 5681.3585 années 80 ## 33 Algeria Africa 1992 67.744 26298373 5023.2166 années 90 ## 34 Algeria Africa 1997 69.152 29072015 4797.2951 années 90 ## 35 Algeria Africa 2002 70.994 31287142 5288.0404 millénaire ## 36 Algeria Africa 2007 72.301 33333216 6223.3675 millénaire ## 37 Angola Africa 1952 30.015 4232095 3520.6103 années 50 ## 38 Angola Africa 1957 31.999 4561361 3827.9405 années 50 ## 39 Angola Africa 1962 34.000 4826015 4269.2767 années 60 ## 40 Angola Africa 1967 35.985 5247469 5522.7764 années 60 ## 41 Angola Africa 1972 37.928 5894858 5473.2880 années 70 ## 42 Angola Africa 1977 39.483 6162675 3008.6474 années 70 ## 43 Angola Africa 1982 39.942 7016384 2756.9537 années 80 ## 44 Angola Africa 1987 39.906 7874230 2430.2083 années 80 ## 45 Angola Africa 1992 40.647 8735988 2627.8457 années 90 ## 46 Angola Africa 1997 40.963 9875024 2277.1409 années 90 ## 47 Angola Africa 2002 41.003 10866106 2773.2873 millénaire ## 48 Angola Africa 2007 42.731 12420476 4797.2313 millénaire ## 49 Argentina Americas 1952 62.485 17876956 5911.3151 années 50 ## 50 Argentina Americas 1957 64.399 19610538 6856.8562 années 50 ## 51 Argentina Americas 1962 65.142 21283783 7133.1660 années 60 ## 52 Argentina Americas 1967 65.634 22934225 8052.9530 années 60 ## 53 Argentina Americas 1972 67.065 24779799 9443.0385 années 70 ## 54 Argentina Americas 1977 68.481 26983828 10079.0267 années 70 ## 55 Argentina Americas 1982 69.942 29341374 8997.8974 années 80 ## 56 Argentina Americas 1987 70.774 31620918 9139.6714 années 80 ## 57 Argentina Americas 1992 71.868 33958947 9308.4187 années 90 ## 58 Argentina Americas 1997 73.275 36203463 10967.2820 années 90 ## 59 Argentina Americas 2002 74.340 38331121 8797.6407 millénaire ## 60 Argentina Americas 2007 75.320 40301927 12779.3796 millénaire ## 61 Australia Oceania 1952 69.120 8691212 10039.5956 années 50 ## 62 Australia Oceania 1957 70.330 9712569 10949.6496 années 50 ## 63 Australia Oceania 1962 70.930 10794968 12217.2269 années 60 ## 64 Australia Oceania 1967 71.100 11872264 14526.1246 années 60 ## 65 Australia Oceania 1972 71.930 13177000 16788.6295 années 70 ## 66 Australia Oceania 1977 73.490 14074100 18334.1975 années 70 ## 67 Australia Oceania 1982 74.740 15184200 19477.0093 années 80 ## 68 Australia Oceania 1987 76.320 16257249 21888.8890 années 80 ## 69 Australia Oceania 1992 77.560 17481977 23424.7668 années 90 ## 70 Australia Oceania 1997 78.830 18565243 26997.9366 années 90 ## 71 Australia Oceania 2002 80.370 19546792 30687.7547 millénaire ## 72 Australia Oceania 2007 81.235 20434176 34435.3674 millénaire ## 73 Austria Europe 1952 66.800 6927772 6137.0765 années 50 ## 74 Austria Europe 1957 67.480 6965860 8842.5980 années 50 ## 75 Austria Europe 1962 69.540 7129864 10750.7211 années 60 ## 76 Austria Europe 1967 70.140 7376998 12834.6024 années 60 ## 77 Austria Europe 1972 70.630 7544201 16661.6256 années 70 ## 78 Austria Europe 1977 72.170 7568430 19749.4223 années 70 ## 79 Austria Europe 1982 73.180 7574613 21597.0836 années 80 ## 80 Austria Europe 1987 74.940 7578903 23687.8261 années 80 ## 81 Austria Europe 1992 76.040 7914969 27042.0187 années 90 ## 82 Austria Europe 1997 77.510 8069876 29095.9207 années 90 ## 83 Austria Europe 2002 78.980 8148312 32417.6077 millénaire ## 84 Austria Europe 2007 79.829 8199783 36126.4927 millénaire ## 85 Bahrain Asia 1952 50.939 120447 9867.0848 années 50 ## 86 Bahrain Asia 1957 53.832 138655 11635.7995 années 50 ## 87 Bahrain Asia 1962 56.923 171863 12753.2751 années 60 ## 88 Bahrain Asia 1967 59.923 202182 14804.6727 années 60 ## 89 Bahrain Asia 1972 63.300 230800 18268.6584 années 70 ## 90 Bahrain Asia 1977 65.593 297410 19340.1020 années 70 ## 91 Bahrain Asia 1982 69.052 377967 19211.1473 années 80 ## 92 Bahrain Asia 1987 70.750 454612 18524.0241 années 80 ## 93 Bahrain Asia 1992 72.601 529491 19035.5792 années 90 ## 94 Bahrain Asia 1997 73.925 598561 20292.0168 années 90 ## 95 Bahrain Asia 2002 74.795 656397 23403.5593 millénaire ## 96 Bahrain Asia 2007 75.635 708573 29796.0483 millénaire ## 97 Bangladesh Asia 1952 37.484 46886859 684.2442 années 50 ## 98 Bangladesh Asia 1957 39.348 51365468 661.6375 années 50 ## 99 Bangladesh Asia 1962 41.216 56839289 686.3416 années 60 ## 100 Bangladesh Asia 1967 43.453 62821884 721.1861 années 60 ## 101 Bangladesh Asia 1972 45.252 70759295 630.2336 années 70 ## 102 Bangladesh Asia 1977 46.923 80428306 659.8772 années 70 ## 103 Bangladesh Asia 1982 50.009 93074406 676.9819 années 80 ## 104 Bangladesh Asia 1987 52.819 103764241 751.9794 années 80 ## 105 Bangladesh Asia 1992 56.018 113704579 837.8102 années 90 ## 106 Bangladesh Asia 1997 59.412 123315288 972.7700 années 90 ## 107 Bangladesh Asia 2002 62.013 135656790 1136.3904 millénaire ## 108 Bangladesh Asia 2007 64.062 150448339 1391.2538 millénaire ## 109 Belgium Europe 1952 68.000 8730405 8343.1051 années 50 ## 110 Belgium Europe 1957 69.240 8989111 9714.9606 années 50 ## 111 Belgium Europe 1962 70.250 9218400 10991.2068 années 60 ## 112 Belgium Europe 1967 70.940 9556500 13149.0412 années 60 ## 113 Belgium Europe 1972 71.440 9709100 16672.1436 années 70 ## 114 Belgium Europe 1977 72.800 9821800 19117.9745 années 70 ## 115 Belgium Europe 1982 73.930 9856303 20979.8459 années 80 ## 116 Belgium Europe 1987 75.350 9870200 22525.5631 années 80 ## 117 Belgium Europe 1992 76.460 10045622 25575.5707 années 90 ## 118 Belgium Europe 1997 77.530 10199787 27561.1966 années 90 ## 119 Belgium Europe 2002 78.320 10311970 30485.8838 millénaire ## 120 Belgium Europe 2007 79.441 10392226 33692.6051 millénaire ## 121 Benin Africa 1952 38.223 1738315 1062.7522 années 50 ## 122 Benin Africa 1957 40.358 1925173 959.6011 années 50 ## 123 Benin Africa 1962 42.618 2151895 949.4991 années 60 ## 124 Benin Africa 1967 44.885 2427334 1035.8314 années 60 ## 125 Benin Africa 1972 47.014 2761407 1085.7969 années 70 ## 126 Benin Africa 1977 49.190 3168267 1029.1613 années 70 ## 127 Benin Africa 1982 50.904 3641603 1277.8976 années 80 ## 128 Benin Africa 1987 52.337 4243788 1225.8560 années 80 ## 129 Benin Africa 1992 53.919 4981671 1191.2077 années 90 ## 130 Benin Africa 1997 54.777 6066080 1232.9753 années 90 ## 131 Benin Africa 2002 54.406 7026113 1372.8779 millénaire ## 132 Benin Africa 2007 56.728 8078314 1441.2849 millénaire ## 133 Bolivia Americas 1952 40.414 2883315 2677.3263 années 50 ## 134 Bolivia Americas 1957 41.890 3211738 2127.6863 années 50 ## 135 Bolivia Americas 1962 43.428 3593918 2180.9725 années 60 ## 136 Bolivia Americas 1967 45.032 4040665 2586.8861 années 60 ## 137 Bolivia Americas 1972 46.714 4565872 2980.3313 années 70 ## 138 Bolivia Americas 1977 50.023 5079716 3548.0978 années 70 ## 139 Bolivia Americas 1982 53.859 5642224 3156.5105 années 80 ## 140 Bolivia Americas 1987 57.251 6156369 2753.6915 années 80 ## 141 Bolivia Americas 1992 59.957 6893451 2961.6997 années 90 ## 142 Bolivia Americas 1997 62.050 7693188 3326.1432 années 90 ## [ reached 'max' / getOption("max.print") -- omitted 1562 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"`
−
+
05
:
00
--- ```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] <code class ='r hljs remark-code'><b><span style="background-color:#FFC0DC">gapminder_2002</span></b> <- filter(gapminder, year == 2002)<br><br><b><span style="background-color:#FFC0DC">gapminder_2002</span></b>_log <- mutate(<b><span style="background-color:#FFC0DC">gapminder_2002</span></b>,<br> log_gdpPercap = log(gdpPercap))</code> --- 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] <code class ='r hljs remark-code'><b><span style="background-color:#FFC0DC">filter(</span></b><b><span style="background-color:#FFDFD1">mutate(gapminder,</span></b> <br> <b><span style="background-color:#FFDFD1">log_gdpPercap = log(gdpPercap))</span></b>, <br> <b><span style="background-color:#FFC0DC">year == 2002)</span></b></code> --- 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] <code class ='r hljs remark-code'><b><span style="background-color:#FFC0DC">gapminder</span></b> |> filter(<b><span style="background-color:#FFC0DC">_____</span></b>, country == "Canada")</code> --- class: title title-1 # Comment procéder avec plusieurs verbes? .box-inv-1[Ces deux lignes donnent le même résultat] <code class ='r hljs remark-code'>filter(<b><span style="background-color:#FFC0DC">gapminder</span></b>, country == "Canada")</code> <code class ='r hljs remark-code'><b><span style="background-color:#FFC0DC">gapminder</span></b> |> filter(country == "Canada")</code> --- 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!] <code class ='r hljs remark-code'>gapminder |> <br> filter(year == 2002) |> <br> mutate(log_gdpPercap = log(gdpPercap))</code> --- class: title title-1 # Améliorer la lisibilité du code avec `|>` <code class ='r hljs remark-code'><b>leave_house</b>(<b>get_dressed</b>(<b>get_out_of_bed</b>(<b>wake_up</b>(<span style="color:#E16462">me</span>, <span style="color:#0D0887">time</span> = <span style="color:#E16462">"8:00"</span>), <span style="color:#0D0887">side</span> = <span style="color:#E16462">"correct"</span>), <span style="color:#0D0887">pants</span> = <span style="color:#E16462">TRUE</span>, <span style="color:#0D0887">shirt</span> = <span style="color:#E16462">TRUE</span>), <span style="color:#0D0887">car</span> = <span style="color:#E16462">TRUE</span>, <span style="color:#0D0887">bike</span> = <span style="color:#E16462">FALSE</span>)</code> -- <code class ='r hljs remark-code'>me |> <br> <b>wake_up</b>(<span style="color:#0D0887">time</span> = <span style="color:#E16462">"8:00"</span>) |> <br> <b>get_out_of_bed</b>(<span style="color:#0D0887">side</span> = <span style="color:#E16462">"correct"</span>) |> <br> <b>get_dressed</b>(<span style="color:#0D0887">pants</span> = <span style="color:#E16462">TRUE</span>, <span style="color:#0D0887">shirt</span> = <span style="color:#E16462">TRUE</span>) |> <br> <b>leave_house</b>(<span style="color:#0D0887">car</span> = <span style="color:#E16462">TRUE</span>, <span style="color:#0D0887">bike</span> = <span style="color:#E16462">FALSE</span>)</code> --- class: title title-1 # `summarize()` .box-inv-1[Créer un tableau résumé] <code class ='r hljs remark-code'><b><span style="background-color:#FFDFD1">gapminder</span></b> |> summarize(<b><span style="background-color:#FFD0CF">mean_life = mean(lifeExp)</span></b>)</code> .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()` <code class ='r hljs remark-code'><b><span style="background-color:#FFDFD1">gapminder</span></b> |> summarize(<b><span style="background-color:#FFD0CF">mean_life = mean(lifeExp),</span></b><br> <b><span style="background-color:#FFD0CF">min_life = min(lifeExp)</span></b>)</code> .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)
−
+
04
:
00
--- ```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.
−
+
04
:
00
--- ```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] <code class ='r hljs remark-code'><b><span style="background-color:#FFDFD1">gapminder</span></b> |> group_by(<b><span style="background-color:#FFD0CF">continent</span></b>)</code> -- .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> ] --- <code class ='r hljs remark-code'><b><span style="background-color:#FFDFD1">pollution</span></b> |> <br> summarize(<b><span style="background-color:#FFD0CF">mean = mean(amount), sum = sum(amount), n = n()</span></b>)</code> .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> ] --- <code class ='r hljs remark-code'><b><span style="background-color:#FFDFD1">pollution</span></b> |> <br> group_by(<b><span style="background-color:#FFD0CF">city</span></b>) |> <br> summarize(<b><span style="background-color:#FFD0CF">mean = mean(amount), sum = sum(amount), n = n()</span></b>)</code> .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: #B2B1F9 !important;"> New York </td> <td style="text-align:left;background-color: #B2B1F9 !important;"> Large </td> <td style="text-align:right;background-color: #B2B1F9 !important;"> 23 </td> </tr> <tr> <td style="text-align:left;background-color: #B2B1F9 !important;"> New York </td> <td style="text-align:left;background-color: #B2B1F9 !important;"> Small </td> <td style="text-align:right;background-color: #B2B1F9 !important;"> 14 </td> </tr> <tr> <td style="text-align:left;background-color: #EFB3FF !important;"> London </td> <td style="text-align:left;background-color: #EFB3FF !important;"> Large </td> <td style="text-align:right;background-color: #EFB3FF !important;"> 22 </td> </tr> <tr> <td style="text-align:left;background-color: #EFB3FF !important;"> London </td> <td style="text-align:left;background-color: #EFB3FF !important;"> Small </td> <td style="text-align:right;background-color: #EFB3FF !important;"> 16 </td> </tr> <tr> <td style="text-align:left;background-color: #FFD0CF !important;"> Beijing </td> <td style="text-align:left;background-color: #FFD0CF !important;"> Large </td> <td style="text-align:right;background-color: #FFD0CF !important;"> 121 </td> </tr> <tr> <td style="text-align:left;background-color: #FFD0CF !important;"> Beijing </td> <td style="text-align:left;background-color: #FFD0CF !important;"> Small </td> <td style="text-align:right;background-color: #FFD0CF !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: #FFD0CF !important;"> Beijing </td> <td style="text-align:right;background-color: #FFD0CF !important;"> 88.5 </td> <td style="text-align:right;background-color: #FFD0CF !important;"> 177 </td> <td style="text-align:right;background-color: #FFD0CF !important;"> 2 </td> </tr> <tr> <td style="text-align:left;background-color: #EFB3FF !important;"> London </td> <td style="text-align:right;background-color: #EFB3FF !important;"> 19.0 </td> <td style="text-align:right;background-color: #EFB3FF !important;"> 38 </td> <td style="text-align:right;background-color: #EFB3FF !important;"> 2 </td> </tr> <tr> <td style="text-align:left;background-color: #B2B1F9 !important;"> New York </td> <td style="text-align:right;background-color: #B2B1F9 !important;"> 18.5 </td> <td style="text-align:right;background-color: #B2B1F9 !important;"> 37 </td> <td style="text-align:right;background-color: #B2B1F9 !important;"> 2 </td> </tr> </tbody> </table> ] --- <code class ='r hljs remark-code'><b><span style="background-color:#FFDFD1">pollution</span></b> |> <br> group_by(<b><span style="background-color:#FFD0CF">particle_size</span></b>) |> <br> summarize(<b><span style="background-color:#FFD0CF">mean = mean(amount), sum = sum(amount), n = n()</span></b>)</code> .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: #FFDFD1 !important;"> New York </td> <td style="text-align:left;background-color: #FFDFD1 !important;"> Large </td> <td style="text-align:right;background-color: #FFDFD1 !important;"> 23 </td> </tr> <tr> <td style="text-align:left;background-color: #FFF0D4 !important;"> New York </td> <td style="text-align:left;background-color: #FFF0D4 !important;"> Small </td> <td style="text-align:right;background-color: #FFF0D4 !important;"> 14 </td> </tr> <tr> <td style="text-align:left;background-color: #FFDFD1 !important;"> London </td> <td style="text-align:left;background-color: #FFDFD1 !important;"> Large </td> <td style="text-align:right;background-color: #FFDFD1 !important;"> 22 </td> </tr> <tr> <td style="text-align:left;background-color: #FFF0D4 !important;"> London </td> <td style="text-align:left;background-color: #FFF0D4 !important;"> Small </td> <td style="text-align:right;background-color: #FFF0D4 !important;"> 16 </td> </tr> <tr> <td style="text-align:left;background-color: #FFDFD1 !important;"> Beijing </td> <td style="text-align:left;background-color: #FFDFD1 !important;"> Large </td> <td style="text-align:right;background-color: #FFDFD1 !important;"> 121 </td> </tr> <tr> <td style="text-align:left;background-color: #FFF0D4 !important;"> Beijing </td> <td style="text-align:left;background-color: #FFF0D4 !important;"> Small </td> <td style="text-align:right;background-color: #FFF0D4 !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: #FFDFD1 !important;"> Large </td> <td style="text-align:right;background-color: #FFDFD1 !important;"> 55.33333 </td> <td style="text-align:right;background-color: #FFDFD1 !important;"> 166 </td> <td style="text-align:right;background-color: #FFDFD1 !important;"> 3 </td> </tr> <tr> <td style="text-align:left;background-color: #FFF0D4 !important;"> Small </td> <td style="text-align:right;background-color: #FFF0D4 !important;"> 28.66667 </td> <td style="text-align:right;background-color: #FFF0D4 !important;"> 86 </td> <td style="text-align:right;background-color: #FFF0D4 !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]
−
+
05
:
00
--- ```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>