{ "cells": [ { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Manipulation avec dplyr" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## le package dplyr\n", "Dans ce cours, afin de manipuler les données, nous allons utiliser la librairie `dplyr` qui assure une manipulation plus intuitive des données. Toutefois, vous pouvez utiliser tout autre libraire ou même les fonctions de base de `R`." ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": true, "jupyter": { "outputs_hidden": true }, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "# install.packages(\"dplyr\")" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "D'abord, téléchargeons un petit _df_ afin d'illustrer la théorie. Dans ce _df_, nous avons les données d'un cycliste qui est sorti un jour d'été faire un petit tour dans l'île de Montréal. Chaque observation, représente le nombre de km parcourus d'un parcours (_lap_), le temps que ça a pris, la vitesse moyenne en `km/h` de chaque _lap_, la puissance moyenne en `watts` et finalement, les battements de cours par minutes.\n" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\n", "
kmtempsvitesseMoyennepuissanceMoyennebpm
1.244:01 19.1 160 134
4.849:42 30.2 133 146
1.021:57 30.8 141 139
17.6136:1129.2 125 144
9.2719:1029.0 121 143
\n" ], "text/latex": [ "\\begin{tabular}{r|lllll}\n", " km & temps & vitesseMoyenne & puissanceMoyenne & bpm\\\\\n", "\\hline\n", "\t 1.24 & 4:01 & 19.1 & 160 & 134 \\\\\n", "\t 4.84 & 9:42 & 30.2 & 133 & 146 \\\\\n", "\t 1.02 & 1:57 & 30.8 & 141 & 139 \\\\\n", "\t 17.61 & 36:11 & 29.2 & 125 & 144 \\\\\n", "\t 9.27 & 19:10 & 29.0 & 121 & 143 \\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "km | temps | vitesseMoyenne | puissanceMoyenne | bpm | \n", "|---|---|---|---|---|\n", "| 1.24 | 4:01 | 19.1 | 160 | 134 | \n", "| 4.84 | 9:42 | 30.2 | 133 | 146 | \n", "| 1.02 | 1:57 | 30.8 | 141 | 139 | \n", "| 17.61 | 36:11 | 29.2 | 125 | 144 | \n", "| 9.27 | 19:10 | 29.0 | 121 | 143 | \n", "\n", "\n" ], "text/plain": [ " km temps vitesseMoyenne puissanceMoyenne bpm\n", "1 1.24 4:01 19.1 160 134\n", "2 4.84 9:42 30.2 133 146\n", "3 1.02 1:57 30.8 141 139\n", "4 17.61 36:11 29.2 125 144\n", "5 9.27 19:10 29.0 121 143" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "df<-read.csv(\"https://raw.githubusercontent.com/nmeraihi/data/master/exemple_2.txt\")\n", "df" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "Il possible d'ordonner les données avec la fonction de base de `R` appelé `order`. Par exemple, on voudrait ordonner notre _df_ en ordre croissant sur la variable `puissanceMoyenne`" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\n", "
kmtempsvitesseMoyennepuissanceMoyennebpm
5 9.2719:1029.0 121 143
417.6136:1129.2 125 144
2 4.849:42 30.2 133 146
3 1.021:57 30.8 141 139
1 1.244:01 19.1 160 134
\n" ], "text/latex": [ "\\begin{tabular}{r|lllll}\n", " & km & temps & vitesseMoyenne & puissanceMoyenne & bpm\\\\\n", "\\hline\n", "\t5 & 9.27 & 19:10 & 29.0 & 121 & 143 \\\\\n", "\t4 & 17.61 & 36:11 & 29.2 & 125 & 144 \\\\\n", "\t2 & 4.84 & 9:42 & 30.2 & 133 & 146 \\\\\n", "\t3 & 1.02 & 1:57 & 30.8 & 141 & 139 \\\\\n", "\t1 & 1.24 & 4:01 & 19.1 & 160 & 134 \\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "| | km | temps | vitesseMoyenne | puissanceMoyenne | bpm | \n", "|---|---|---|---|---|\n", "| 5 | 9.27 | 19:10 | 29.0 | 121 | 143 | \n", "| 4 | 17.61 | 36:11 | 29.2 | 125 | 144 | \n", "| 2 | 4.84 | 9:42 | 30.2 | 133 | 146 | \n", "| 3 | 1.02 | 1:57 | 30.8 | 141 | 139 | \n", "| 1 | 1.24 | 4:01 | 19.1 | 160 | 134 | \n", "\n", "\n" ], "text/plain": [ " km temps vitesseMoyenne puissanceMoyenne bpm\n", "5 9.27 19:10 29.0 121 143\n", "4 17.61 36:11 29.2 125 144\n", "2 4.84 9:42 30.2 133 146\n", "3 1.02 1:57 30.8 141 139\n", "1 1.24 4:01 19.1 160 134" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "df[order(df$puissanceMoyenne),]" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "Par défaut, l'ordre est est croissant, on peut le rendre décroissant en ajoutant l'argument `decreasing = T`" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\n", "
kmtempsvitesseMoyennepuissanceMoyennebpm
3 1.021:57 30.8 141 139
2 4.849:42 30.2 133 146
417.6136:1129.2 125 144
5 9.2719:1029.0 121 143
1 1.244:01 19.1 160 134
\n" ], "text/latex": [ "\\begin{tabular}{r|lllll}\n", " & km & temps & vitesseMoyenne & puissanceMoyenne & bpm\\\\\n", "\\hline\n", "\t3 & 1.02 & 1:57 & 30.8 & 141 & 139 \\\\\n", "\t2 & 4.84 & 9:42 & 30.2 & 133 & 146 \\\\\n", "\t4 & 17.61 & 36:11 & 29.2 & 125 & 144 \\\\\n", "\t5 & 9.27 & 19:10 & 29.0 & 121 & 143 \\\\\n", "\t1 & 1.24 & 4:01 & 19.1 & 160 & 134 \\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "| | km | temps | vitesseMoyenne | puissanceMoyenne | bpm | \n", "|---|---|---|---|---|\n", "| 3 | 1.02 | 1:57 | 30.8 | 141 | 139 | \n", "| 2 | 4.84 | 9:42 | 30.2 | 133 | 146 | \n", "| 4 | 17.61 | 36:11 | 29.2 | 125 | 144 | \n", "| 5 | 9.27 | 19:10 | 29.0 | 121 | 143 | \n", "| 1 | 1.24 | 4:01 | 19.1 | 160 | 134 | \n", "\n", "\n" ], "text/plain": [ " km temps vitesseMoyenne puissanceMoyenne bpm\n", "3 1.02 1:57 30.8 141 139\n", "2 4.84 9:42 30.2 133 146\n", "4 17.61 36:11 29.2 125 144\n", "5 9.27 19:10 29.0 121 143\n", "1 1.24 4:01 19.1 160 134" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "df[order(df$vitesseMoyenne, decreasing = T),]" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "Toutefois, lorsque nous avons une base de données comportant un nombre plus important de variables, la syntaxe peut devenir plus compliquée et lourde d’écriture. Regardons un autre exemple:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\n", "
numeropoldebut_polfin_polfreq_paiementcout6cout7nbsinequipe
4 11-4-199610-4-199712 NA NA 0 3
4 11-4-199710-4-199812 NA NA 0 3
4 11-4-200217-7-200212 NA NA 0 3
4 11-4-200310-4-200412 NA NA 0 3
12 3-5-1995 2-5-1996 1 NA NA 0 3
\n" ], "text/latex": [ "\\begin{tabular}{r|lllllllllllllllllllllllllll}\n", " numeropol & debut\\_pol & fin\\_pol & freq\\_paiement & ⋯ & cout6 & cout7 & nbsin & equipe\\\\\n", "\\hline\n", "\t 4 & 11-4-1996 & 10-4-1997 & 12 & ⋯ & NA & NA & 0 & 3 \\\\\n", "\t 4 & 11-4-1997 & 10-4-1998 & 12 & ⋯ & NA & NA & 0 & 3 \\\\\n", "\t 4 & 11-4-2002 & 17-7-2002 & 12 & ⋯ & NA & NA & 0 & 3 \\\\\n", "\t ⋮ & ⋮ & ⋮ & ⋮ & ⋱ & ⋮ & ⋮ & ⋮ & ⋮\\\\\n", "\t 4 & 11-4-2003 & 10-4-2004 & 12 & ⋯ & NA & NA & 0 & 3 \\\\\n", "\t 12 & 3-5-1995 & 2-5-1996 & 1 & ⋯ & NA & NA & 0 & 3 \\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "numeropol | debut_pol | fin_pol | freq_paiement | ⋯ | cout6 | cout7 | nbsin | equipe | \n", "|---|---|---|---|---|---|\n", "| 4 | 11-4-1996 | 10-4-1997 | 12 | ⋯ | NA | NA | 0 | 3 | \n", "| 4 | 11-4-1997 | 10-4-1998 | 12 | ⋯ | NA | NA | 0 | 3 | \n", "| 4 | 11-4-2002 | 17-7-2002 | 12 | ⋯ | NA | NA | 0 | 3 | \n", "| ⋮ | ⋮ | ⋮ | ⋮ | ⋱ | ⋮ | ⋮ | ⋮ | ⋮ | \n", "| 4 | 11-4-2003 | 10-4-2004 | 12 | ⋯ | NA | NA | 0 | 3 | \n", "| 12 | 3-5-1995 | 2-5-1996 | 1 | ⋯ | NA | NA | 0 | 3 | \n", "\n", "\n" ], "text/plain": [ " numeropol debut_pol fin_pol freq_paiement ⋯ cout6 cout7 nbsin equipe\n", "1 4 11-4-1996 10-4-1997 12 ⋯ NA NA 0 3 \n", "2 4 11-4-1997 10-4-1998 12 ⋯ NA NA 0 3 \n", "3 4 11-4-2002 17-7-2002 12 ⋯ NA NA 0 3 \n", "⋮ ⋮ ⋮ ⋮ ⋮ ⋱ ⋮ ⋮ ⋮ ⋮ \n", "5 4 11-4-2003 10-4-2004 12 ⋯ NA NA 0 3 \n", "6 12 3-5-1995 2-5-1996 1 ⋯ NA NA 0 3 " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "df_ass <-read.csv(\"https://raw.githubusercontent.com/nmeraihi/data/master/assurance.csv\", header = T)\n", "head(df_ass)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Affichons notre base de données en ordre croissant sur le nombre de sinistres et le numéro de police;" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\n", "
numeropoldebut_polfin_polfreq_paiementcout6cout7nbsinequipe
9882006 13-6-199612-6-199712 NA NA 2 3
9021820 1-10-199630-9-1997 1 NA NA 2 3
8611733 10-5-19989-5-1999 1 NA NA 2 3
44 18-7-200210-4-200312 NA NA 0 3
54 11-4-200310-4-200412 NA NA 0 3
\n" ], "text/latex": [ "\\begin{tabular}{r|lllllllllllllllllllllllllll}\n", " & numeropol & debut\\_pol & fin\\_pol & freq\\_paiement & ⋯ & cout6 & cout7 & nbsin & equipe\\\\\n", "\\hline\n", "\t988 & 2006 & 13-6-1996 & 12-6-1997 & 12 & ⋯ & NA & NA & 2 & 3 \\\\\n", "\t902 & 1820 & 1-10-1996 & 30-9-1997 & 1 & ⋯ & NA & NA & 2 & 3 \\\\\n", "\t861 & 1733 & 10-5-1998 & 9-5-1999 & 1 & ⋯ & NA & NA & 2 & 3 \\\\\n", "\t⋮ & ⋮ & ⋮ & ⋮ & ⋮ & ⋱ & ⋮ & ⋮ & ⋮ & ⋮\\\\\n", "\t4 & 4 & 18-7-2002 & 10-4-2003 & 12 & ⋯ & NA & NA & 0 & 3 \\\\\n", "\t5 & 4 & 11-4-2003 & 10-4-2004 & 12 & ⋯ & NA & NA & 0 & 3 \\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "| | numeropol | debut_pol | fin_pol | freq_paiement | ⋯ | cout6 | cout7 | nbsin | equipe | \n", "|---|---|---|---|---|---|\n", "| 988 | 2006 | 13-6-1996 | 12-6-1997 | 12 | ⋯ | NA | NA | 2 | 3 | \n", "| 902 | 1820 | 1-10-1996 | 30-9-1997 | 1 | ⋯ | NA | NA | 2 | 3 | \n", "| 861 | 1733 | 10-5-1998 | 9-5-1999 | 1 | ⋯ | NA | NA | 2 | 3 | \n", "| ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋱ | ⋮ | ⋮ | ⋮ | ⋮ | \n", "| 4 | 4 | 18-7-2002 | 10-4-2003 | 12 | ⋯ | NA | NA | 0 | 3 | \n", "| 5 | 4 | 11-4-2003 | 10-4-2004 | 12 | ⋯ | NA | NA | 0 | 3 | \n", "\n", "\n" ], "text/plain": [ " numeropol debut_pol fin_pol freq_paiement ⋯ cout6 cout7 nbsin equipe\n", "988 2006 13-6-1996 12-6-1997 12 ⋯ NA NA 2 3 \n", "902 1820 1-10-1996 30-9-1997 1 ⋯ NA NA 2 3 \n", "861 1733 10-5-1998 9-5-1999 1 ⋯ NA NA 2 3 \n", "⋮ ⋮ ⋮ ⋮ ⋮ ⋱ ⋮ ⋮ ⋮ ⋮ \n", "4 4 18-7-2002 10-4-2003 12 ⋯ NA NA 0 3 \n", "5 4 11-4-2003 10-4-2004 12 ⋯ NA NA 0 3 " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "df_ass[order(df_ass$nbsin, df_ass$numeropol, decreasing = T),]" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Dans le tableau affiché ci-haut, on voit bien que cette fonction ne nous permet pas d'appliquer un ordre croissant ou décroissant sur une variable précise" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## arrange" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "Maintenant, utilisons le paquet (_package_) `dplyr` qui nous permet de plus facilement d'appliquer un ordre quelconque sur une variable précise indépendamment des autres variables;" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": true, "jupyter": { "outputs_hidden": true }, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "library(dplyr, warn.conflicts = FALSE)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\n", "
numeropoldebut_polfin_polfreq_paiementcout6cout7nbsinequipe
71 15-2-1996 14-2-1997 1 NA NA 2 3
79 20-11-199721-6-1998 12 NA NA 2 3
116 4-9-1998 11-6-1999 1 NA NA 2 3
2036 14-3-200026-2-200112 NA NA 0 3
2036 27-2-200113-3-200112 NA NA 0 3
\n" ], "text/latex": [ "\\begin{tabular}{r|lllllllllllllllllllllllllll}\n", " numeropol & debut\\_pol & fin\\_pol & freq\\_paiement & ⋯ & cout6 & cout7 & nbsin & equipe\\\\\n", "\\hline\n", "\t 71 & 15-2-1996 & 14-2-1997 & 1 & ⋯ & NA & NA & 2 & 3 \\\\\n", "\t 79 & 20-11-1997 & 21-6-1998 & 12 & ⋯ & NA & NA & 2 & 3 \\\\\n", "\t 116 & 4-9-1998 & 11-6-1999 & 1 & ⋯ & NA & NA & 2 & 3 \\\\\n", "\t ⋮ & ⋮ & ⋮ & ⋮ & ⋱ & ⋮ & ⋮ & ⋮ & ⋮\\\\\n", "\t 2036 & 14-3-2000 & 26-2-2001 & 12 & ⋯ & NA & NA & 0 & 3 \\\\\n", "\t 2036 & 27-2-2001 & 13-3-2001 & 12 & ⋯ & NA & NA & 0 & 3 \\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "numeropol | debut_pol | fin_pol | freq_paiement | ⋯ | cout6 | cout7 | nbsin | equipe | \n", "|---|---|---|---|---|---|\n", "| 71 | 15-2-1996 | 14-2-1997 | 1 | ⋯ | NA | NA | 2 | 3 | \n", "| 79 | 20-11-1997 | 21-6-1998 | 12 | ⋯ | NA | NA | 2 | 3 | \n", "| 116 | 4-9-1998 | 11-6-1999 | 1 | ⋯ | NA | NA | 2 | 3 | \n", "| ⋮ | ⋮ | ⋮ | ⋮ | ⋱ | ⋮ | ⋮ | ⋮ | ⋮ | \n", "| 2036 | 14-3-2000 | 26-2-2001 | 12 | ⋯ | NA | NA | 0 | 3 | \n", "| 2036 | 27-2-2001 | 13-3-2001 | 12 | ⋯ | NA | NA | 0 | 3 | \n", "\n", "\n" ], "text/plain": [ " numeropol debut_pol fin_pol freq_paiement ⋯ cout6 cout7 nbsin equipe\n", "1 71 15-2-1996 14-2-1997 1 ⋯ NA NA 2 3 \n", "2 79 20-11-1997 21-6-1998 12 ⋯ NA NA 2 3 \n", "3 116 4-9-1998 11-6-1999 1 ⋯ NA NA 2 3 \n", "⋮ ⋮ ⋮ ⋮ ⋮ ⋱ ⋮ ⋮ ⋮ ⋮ \n", "1000 2036 14-3-2000 26-2-2001 12 ⋯ NA NA 0 3 \n", "1001 2036 27-2-2001 13-3-2001 12 ⋯ NA NA 0 3 " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "arrange(df_ass, desc(nbsin), numeropol)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## select" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Ce paquet nous permet aussi de sélectionner des variables d'intérêt. Par exemple, dans notre `df_ass`, on désire seulement sélectionner les variables `numeropol`, `type_territoire` et `nbsin`" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\n", "
numeropoltype_territoirenbsin
4 Semi-urbain0
4 Semi-urbain0
4 Semi-urbain0
2036 Semi-urbain0
2036 Semi-urbain1
\n" ], "text/latex": [ "\\begin{tabular}{r|lll}\n", " numeropol & type\\_territoire & nbsin\\\\\n", "\\hline\n", "\t 4 & Semi-urbain & 0 \\\\\n", "\t 4 & Semi-urbain & 0 \\\\\n", "\t 4 & Semi-urbain & 0 \\\\\n", "\t ⋮ & ⋮ & ⋮\\\\\n", "\t 2036 & Semi-urbain & 0 \\\\\n", "\t 2036 & Semi-urbain & 1 \\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "numeropol | type_territoire | nbsin | \n", "|---|---|---|---|---|---|\n", "| 4 | Semi-urbain | 0 | \n", "| 4 | Semi-urbain | 0 | \n", "| 4 | Semi-urbain | 0 | \n", "| ⋮ | ⋮ | ⋮ | \n", "| 2036 | Semi-urbain | 0 | \n", "| 2036 | Semi-urbain | 1 | \n", "\n", "\n" ], "text/plain": [ " numeropol type_territoire nbsin\n", "1 4 Semi-urbain 0 \n", "2 4 Semi-urbain 0 \n", "3 4 Semi-urbain 0 \n", "⋮ ⋮ ⋮ ⋮ \n", "1000 2036 Semi-urbain 0 \n", "1001 2036 Semi-urbain 1 " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "select(df_ass, numeropol,type_territoire, nbsin)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## filter" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Afin de filtrer des données sur des observations d'intérêt. On peut utiliser la fonction de base de **R** `which`. Par exemple dans les données `Cars93` du package `MASS`, on voudrait extraire les véhicules ayant 8 cylindres. On voudrait également afficher que les deux variables `'Horsepower'` et `'Passengers'`" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": true, "jupyter": { "outputs_hidden": true }, "slideshow": { "slide_type": "subslide" } }, "outputs": [], "source": [ "library(MASS, warn.conflicts = F)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\n", "
HorsepowerPassengers
102006
112955
181706
482785
522106
\n" ], "text/latex": [ "\\begin{tabular}{r|ll}\n", " & Horsepower & Passengers\\\\\n", "\\hline\n", "\t10 & 200 & 6 \\\\\n", "\t11 & 295 & 5 \\\\\n", "\t18 & 170 & 6 \\\\\n", "\t⋮ & ⋮ & ⋮\\\\\n", "\t48 & 278 & 5 \\\\\n", "\t52 & 210 & 6 \\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "| | Horsepower | Passengers | \n", "|---|---|---|---|---|---|\n", "| 10 | 200 | 6 | \n", "| 11 | 295 | 5 | \n", "| 18 | 170 | 6 | \n", "| ⋮ | ⋮ | ⋮ | \n", "| 48 | 278 | 5 | \n", "| 52 | 210 | 6 | \n", "\n", "\n" ], "text/plain": [ " Horsepower Passengers\n", "10 200 6 \n", "11 295 5 \n", "18 170 6 \n", "⋮ ⋮ ⋮ \n", "48 278 5 \n", "52 210 6 " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "Cars93[which(Cars93$Cylinders==8), c('Horsepower' , 'Passengers')]" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "Toutefois, la fonction `filter` de la librairie `dyplr` est plus flexible lorsqu'il s'agit d'appliquer des filtres plus complexes. Essayons le même exemple avec cette fonction;" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\n", "
HorsepowerPassengers
2006
2955
1706
2785
2106
\n" ], "text/latex": [ "\\begin{tabular}{r|ll}\n", " Horsepower & Passengers\\\\\n", "\\hline\n", "\t 200 & 6 \\\\\n", "\t 295 & 5 \\\\\n", "\t 170 & 6 \\\\\n", "\t ⋮ & ⋮\\\\\n", "\t 278 & 5 \\\\\n", "\t 210 & 6 \\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "Horsepower | Passengers | \n", "|---|---|---|---|---|---|\n", "| 200 | 6 | \n", "| 295 | 5 | \n", "| 170 | 6 | \n", "| ⋮ | ⋮ | \n", "| 278 | 5 | \n", "| 210 | 6 | \n", "\n", "\n" ], "text/plain": [ " Horsepower Passengers\n", "1 200 6 \n", "2 295 5 \n", "3 170 6 \n", "⋮ ⋮ ⋮ \n", "6 278 5 \n", "7 210 6 " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "filter(Cars93, Cylinders==8)[c('Horsepower' , 'Passengers')]" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "Si l'on cherche les médecins qui ont eu deux sinistres dans notre base de données `df_ass`;" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\t\n", "\n", "
numeropoldebut_polfin_polfreq_paiementcout6cout7nbsinequipe
71 15-2-199614-2-1997 1 NA NA 2 3
140 15-4-199514-4-199612 NA NA 2 3
1820 1-10-199630-9-1997 1 NA NA 2 3
\n" ], "text/latex": [ "\\begin{tabular}{r|lllllllllllllllllllllllllll}\n", " numeropol & debut\\_pol & fin\\_pol & freq\\_paiement & ⋯ & cout6 & cout7 & nbsin & equipe\\\\\n", "\\hline\n", "\t 71 & 15-2-1996 & 14-2-1997 & 1 & ⋯ & NA & NA & 2 & 3 \\\\\n", "\t 140 & 15-4-1995 & 14-4-1996 & 12 & ⋯ & NA & NA & 2 & 3 \\\\\n", "\t 1820 & 1-10-1996 & 30-9-1997 & 1 & ⋯ & NA & NA & 2 & 3 \\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "numeropol | debut_pol | fin_pol | freq_paiement | ⋯ | cout6 | cout7 | nbsin | equipe | \n", "|---|---|---|\n", "| 71 | 15-2-1996 | 14-2-1997 | 1 | ⋯ | NA | NA | 2 | 3 | \n", "| 140 | 15-4-1995 | 14-4-1996 | 12 | ⋯ | NA | NA | 2 | 3 | \n", "| 1820 | 1-10-1996 | 30-9-1997 | 1 | ⋯ | NA | NA | 2 | 3 | \n", "\n", "\n" ], "text/plain": [ " numeropol debut_pol fin_pol freq_paiement ⋯ cout6 cout7 nbsin equipe\n", "1 71 15-2-1996 14-2-1997 1 ⋯ NA NA 2 3 \n", "2 140 15-4-1995 14-4-1996 12 ⋯ NA NA 2 3 \n", "3 1820 1-10-1996 30-9-1997 1 ⋯ NA NA 2 3 " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "filter(df_ass, nbsin==2, type_prof==\"Médecin\")" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## mutate" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Dans ce package, on trouve aussi la fonction `mutate` qui permet d'ajouter de nouvelles variables à notre df" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\n", "
kmtempsvitesseMoyennepuissanceMoyennebpmarrondi
1.244:01 19.1 160 134 19
4.849:42 30.2 133 146 30
1.021:57 30.8 141 139 31
17.6136:1129.2 125 144 29
9.2719:1029.0 121 143 29
\n" ], "text/latex": [ "\\begin{tabular}{r|llllll}\n", " km & temps & vitesseMoyenne & puissanceMoyenne & bpm & arrondi\\\\\n", "\\hline\n", "\t 1.24 & 4:01 & 19.1 & 160 & 134 & 19 \\\\\n", "\t 4.84 & 9:42 & 30.2 & 133 & 146 & 30 \\\\\n", "\t 1.02 & 1:57 & 30.8 & 141 & 139 & 31 \\\\\n", "\t 17.61 & 36:11 & 29.2 & 125 & 144 & 29 \\\\\n", "\t 9.27 & 19:10 & 29.0 & 121 & 143 & 29 \\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "km | temps | vitesseMoyenne | puissanceMoyenne | bpm | arrondi | \n", "|---|---|---|---|---|\n", "| 1.24 | 4:01 | 19.1 | 160 | 134 | 19 | \n", "| 4.84 | 9:42 | 30.2 | 133 | 146 | 30 | \n", "| 1.02 | 1:57 | 30.8 | 141 | 139 | 31 | \n", "| 17.61 | 36:11 | 29.2 | 125 | 144 | 29 | \n", "| 9.27 | 19:10 | 29.0 | 121 | 143 | 29 | \n", "\n", "\n" ], "text/plain": [ " km temps vitesseMoyenne puissanceMoyenne bpm arrondi\n", "1 1.24 4:01 19.1 160 134 19 \n", "2 4.84 9:42 30.2 133 146 30 \n", "3 1.02 1:57 30.8 141 139 31 \n", "4 17.61 36:11 29.2 125 144 29 \n", "5 9.27 19:10 29.0 121 143 29 " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "mutate(df, arrondi=round(df$vitesseMoyenne,0))" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "Ajoutons maintenant trois nouvlles variables;" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\n", "
kmtempsvitesseMoyennepuissanceMoyennebpmarrondisegementStravaarrondi_2
1.24 4:01 19.1 160 134 19 segment_1 9.5
4.84 9:42 30.2 133 146 30 segment_215.0
1.02 1:57 30.8 141 139 31 segment_315.5
17.61 36:11 29.2 125 144 29 segment_414.5
9.27 19:10 29.0 121 143 29 segment_514.5
\n" ], "text/latex": [ "\\begin{tabular}{r|llllllll}\n", " km & temps & vitesseMoyenne & puissanceMoyenne & bpm & arrondi & segementStrava & arrondi\\_2\\\\\n", "\\hline\n", "\t 1.24 & 4:01 & 19.1 & 160 & 134 & 19 & segment\\_1 & 9.5 \\\\\n", "\t 4.84 & 9:42 & 30.2 & 133 & 146 & 30 & segment\\_2 & 15.0 \\\\\n", "\t 1.02 & 1:57 & 30.8 & 141 & 139 & 31 & segment\\_3 & 15.5 \\\\\n", "\t 17.61 & 36:11 & 29.2 & 125 & 144 & 29 & segment\\_4 & 14.5 \\\\\n", "\t 9.27 & 19:10 & 29.0 & 121 & 143 & 29 & segment\\_5 & 14.5 \\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "km | temps | vitesseMoyenne | puissanceMoyenne | bpm | arrondi | segementStrava | arrondi_2 | \n", "|---|---|---|---|---|\n", "| 1.24 | 4:01 | 19.1 | 160 | 134 | 19 | segment_1 | 9.5 | \n", "| 4.84 | 9:42 | 30.2 | 133 | 146 | 30 | segment_2 | 15.0 | \n", "| 1.02 | 1:57 | 30.8 | 141 | 139 | 31 | segment_3 | 15.5 | \n", "| 17.61 | 36:11 | 29.2 | 125 | 144 | 29 | segment_4 | 14.5 | \n", "| 9.27 | 19:10 | 29.0 | 121 | 143 | 29 | segment_5 | 14.5 | \n", "\n", "\n" ], "text/plain": [ " km temps vitesseMoyenne puissanceMoyenne bpm arrondi segementStrava\n", "1 1.24 4:01 19.1 160 134 19 segment_1 \n", "2 4.84 9:42 30.2 133 146 30 segment_2 \n", "3 1.02 1:57 30.8 141 139 31 segment_3 \n", "4 17.61 36:11 29.2 125 144 29 segment_4 \n", "5 9.27 19:10 29.0 121 143 29 segment_5 \n", " arrondi_2\n", "1 9.5 \n", "2 15.0 \n", "3 15.5 \n", "4 14.5 \n", "5 14.5 " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "mutate(df, arrondi=round(df$vitesseMoyenne,0), segementStrava=paste(\"segment\",1:5,sep = \"_\"), arrondi_2=arrondi/2)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## summarize" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "La fonction `summarize` est très similaire à la fonction `mutate`. Toutefois, contrairement à `mutate`, la fonction `summarize` ne travaille pas sur une copie du df, mais elle crée un tout nouveau df avec les nouvelles variables. " ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\n", "
TotalKmParcour
33.98
\n" ], "text/latex": [ "\\begin{tabular}{r|l}\n", " TotalKmParcour\\\\\n", "\\hline\n", "\t 33.98\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "TotalKmParcour | \n", "|---|\n", "| 33.98 | \n", "\n", "\n" ], "text/plain": [ " TotalKmParcour\n", "1 33.98 " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "summarise(df,TotalKmParcour=sum(km))" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\n", "
TotalKmParcourvitesseMoyennepuissanceMoyenne
33.9827.66136
\n" ], "text/latex": [ "\\begin{tabular}{r|lll}\n", " TotalKmParcour & vitesseMoyenne & puissanceMoyenne\\\\\n", "\\hline\n", "\t 33.98 & 27.66 & 136 \\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "TotalKmParcour | vitesseMoyenne | puissanceMoyenne | \n", "|---|\n", "| 33.98 | 27.66 | 136 | \n", "\n", "\n" ], "text/plain": [ " TotalKmParcour vitesseMoyenne puissanceMoyenne\n", "1 33.98 27.66 136 " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "summarise(df,TotalKmParcour=sum(km), vitesseMoyenne= mean(df$vitesseMoyenne), puissanceMoyenne=mean(df$puissanceMoyenne))" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "On voit bien que l'écriture du code commence à être un peu plus compliquée lorsque nous avons plusieurs parenthèses dans notre fonction. Pour remédier à ce problème, nous verrons la notion de _piiping_;" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## L'opérateur Pipe: %>%" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "Avant d'aller plus loin, introduisons l'opérateur de _pipe_:%>%. **dplyr** importe cet opérateur d'une autre librairie (`magrittr`). Cet opérateur vous permet de diriger la sortie d'une fonction vers l'entrée d'une autre fonction. Au lieu d'imbriquer des fonctions (lecture de l'intérieur vers l'extérieur), l'idée de _piping_ est de lire les fonctions de gauche à droite." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "![piping data](http://res.cloudinary.com/dyd911kmh/image/upload/f_auto,q_auto:best/v1510846626/Pipe-Mathematical_gczmab.png)\n", "\n", "Crédit de l'image [Pipes in R Tutorial For Beginners](https://www.datacamp.com/community/tutorials/pipe-r-tutorial)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "Lorsque nous avons écrit:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\n", "
numeropoltype_territoirenbsin
4 Semi-urbain0
4 Semi-urbain0
4 Semi-urbain0
2036 Semi-urbain0
2036 Semi-urbain1
\n" ], "text/latex": [ "\\begin{tabular}{r|lll}\n", " numeropol & type\\_territoire & nbsin\\\\\n", "\\hline\n", "\t 4 & Semi-urbain & 0 \\\\\n", "\t 4 & Semi-urbain & 0 \\\\\n", "\t 4 & Semi-urbain & 0 \\\\\n", "\t ⋮ & ⋮ & ⋮\\\\\n", "\t 2036 & Semi-urbain & 0 \\\\\n", "\t 2036 & Semi-urbain & 1 \\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "numeropol | type_territoire | nbsin | \n", "|---|---|---|---|---|---|\n", "| 4 | Semi-urbain | 0 | \n", "| 4 | Semi-urbain | 0 | \n", "| 4 | Semi-urbain | 0 | \n", "| ⋮ | ⋮ | ⋮ | \n", "| 2036 | Semi-urbain | 0 | \n", "| 2036 | Semi-urbain | 1 | \n", "\n", "\n" ], "text/plain": [ " numeropol type_territoire nbsin\n", "1 4 Semi-urbain 0 \n", "2 4 Semi-urbain 0 \n", "3 4 Semi-urbain 0 \n", "⋮ ⋮ ⋮ ⋮ \n", "1000 2036 Semi-urbain 0 \n", "1001 2036 Semi-urbain 1 " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "select(df_ass, numeropol, type_territoire, nbsin)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "Si on lit ce que nous avons écrit précédemment de l'intérieur vert l'extérieur, en utilisant le _piping_, nous aurons ceci:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\n", "
numeropoltype_territoirenbsin
4 Semi-urbain0
4 Semi-urbain0
4 Semi-urbain0
2036 Semi-urbain0
2036 Semi-urbain1
\n" ], "text/latex": [ "\\begin{tabular}{r|lll}\n", " numeropol & type\\_territoire & nbsin\\\\\n", "\\hline\n", "\t 4 & Semi-urbain & 0 \\\\\n", "\t 4 & Semi-urbain & 0 \\\\\n", "\t 4 & Semi-urbain & 0 \\\\\n", "\t ⋮ & ⋮ & ⋮\\\\\n", "\t 2036 & Semi-urbain & 0 \\\\\n", "\t 2036 & Semi-urbain & 1 \\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "numeropol | type_territoire | nbsin | \n", "|---|---|---|---|---|---|\n", "| 4 | Semi-urbain | 0 | \n", "| 4 | Semi-urbain | 0 | \n", "| 4 | Semi-urbain | 0 | \n", "| ⋮ | ⋮ | ⋮ | \n", "| 2036 | Semi-urbain | 0 | \n", "| 2036 | Semi-urbain | 1 | \n", "\n", "\n" ], "text/plain": [ " numeropol type_territoire nbsin\n", "1 4 Semi-urbain 0 \n", "2 4 Semi-urbain 0 \n", "3 4 Semi-urbain 0 \n", "⋮ ⋮ ⋮ ⋮ \n", "1000 2036 Semi-urbain 0 \n", "1001 2036 Semi-urbain 1 " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "df_ass %>%\n", " select (numeropol,type_territoire, nbsin)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "ou;" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\n", "
numeropoltype_territoirenbsin
4 Semi-urbain0
4 Semi-urbain0
4 Semi-urbain0
4 Semi-urbain0
12 Semi-urbain0
\n" ], "text/latex": [ "\\begin{tabular}{r|lll}\n", " numeropol & type\\_territoire & nbsin\\\\\n", "\\hline\n", "\t 4 & Semi-urbain & 0 \\\\\n", "\t 4 & Semi-urbain & 0 \\\\\n", "\t 4 & Semi-urbain & 0 \\\\\n", "\t ⋮ & ⋮ & ⋮\\\\\n", "\t 4 & Semi-urbain & 0 \\\\\n", "\t 12 & Semi-urbain & 0 \\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "numeropol | type_territoire | nbsin | \n", "|---|---|---|---|---|---|\n", "| 4 | Semi-urbain | 0 | \n", "| 4 | Semi-urbain | 0 | \n", "| 4 | Semi-urbain | 0 | \n", "| ⋮ | ⋮ | ⋮ | \n", "| 4 | Semi-urbain | 0 | \n", "| 12 | Semi-urbain | 0 | \n", "\n", "\n" ], "text/plain": [ " numeropol type_territoire nbsin\n", "1 4 Semi-urbain 0 \n", "2 4 Semi-urbain 0 \n", "3 4 Semi-urbain 0 \n", "⋮ ⋮ ⋮ ⋮ \n", "5 4 Semi-urbain 0 \n", "6 12 Semi-urbain 0 " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "df_ass %>%\n", " select (numeropol,type_territoire, nbsin) %>%\n", " head" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## group_by" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Nous pouvons aussi grouper les données comme nous le faisions dans `SAS` avec les `PROC SQL`" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": true, "jupyter": { "outputs_hidden": true }, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "df_ass$coutTot<-rowSums(df_ass[,c(19:25)], na.rm = T, dims = 1)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\n", "
numeropoldebut_polfin_polfreq_paiementcout7nbsinequipecoutTot
4 11-4-199610-4-199712 NA 0 3 0
4 11-4-199710-4-199812 NA 0 3 0
4 11-4-200217-7-200212 NA 0 3 0
2036 27-2-200113-3-200112 NA 0 3 0.0
2036 14-3-200113-3-200212 NA 1 3 231051.8
\n" ], "text/latex": [ "\\begin{tabular}{r|llllllllllllllllllllllllllll}\n", " numeropol & debut\\_pol & fin\\_pol & freq\\_paiement & ⋯ & cout7 & nbsin & equipe & coutTot\\\\\n", "\\hline\n", "\t 4 & 11-4-1996 & 10-4-1997 & 12 & ⋯ & NA & 0 & 3 & 0 \\\\\n", "\t 4 & 11-4-1997 & 10-4-1998 & 12 & ⋯ & NA & 0 & 3 & 0 \\\\\n", "\t 4 & 11-4-2002 & 17-7-2002 & 12 & ⋯ & NA & 0 & 3 & 0 \\\\\n", "\t ⋮ & ⋮ & ⋮ & ⋮ & ⋱ & ⋮ & ⋮ & ⋮ & ⋮\\\\\n", "\t 2036 & 27-2-2001 & 13-3-2001 & 12 & ⋯ & NA & 0 & 3 & 0.0 \\\\\n", "\t 2036 & 14-3-2001 & 13-3-2002 & 12 & ⋯ & NA & 1 & 3 & 231051.8 \\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "numeropol | debut_pol | fin_pol | freq_paiement | ⋯ | cout7 | nbsin | equipe | coutTot | \n", "|---|---|---|---|---|---|\n", "| 4 | 11-4-1996 | 10-4-1997 | 12 | ⋯ | NA | 0 | 3 | 0 | \n", "| 4 | 11-4-1997 | 10-4-1998 | 12 | ⋯ | NA | 0 | 3 | 0 | \n", "| 4 | 11-4-2002 | 17-7-2002 | 12 | ⋯ | NA | 0 | 3 | 0 | \n", "| ⋮ | ⋮ | ⋮ | ⋮ | ⋱ | ⋮ | ⋮ | ⋮ | ⋮ | \n", "| 2036 | 27-2-2001 | 13-3-2001 | 12 | ⋯ | NA | 0 | 3 | 0.0 | \n", "| 2036 | 14-3-2001 | 13-3-2002 | 12 | ⋯ | NA | 1 | 3 | 231051.8 | \n", "\n", "\n" ], "text/plain": [ " numeropol debut_pol fin_pol freq_paiement ⋯ cout7 nbsin equipe coutTot \n", "1 4 11-4-1996 10-4-1997 12 ⋯ NA 0 3 0 \n", "2 4 11-4-1997 10-4-1998 12 ⋯ NA 0 3 0 \n", "3 4 11-4-2002 17-7-2002 12 ⋯ NA 0 3 0 \n", "⋮ ⋮ ⋮ ⋮ ⋮ ⋱ ⋮ ⋮ ⋮ ⋮ \n", "1000 2036 27-2-2001 13-3-2001 12 ⋯ NA 0 3 0.0\n", "1001 2036 14-3-2001 13-3-2002 12 ⋯ NA 1 3 231051.8" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "df_ass" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\n", "
TotalNbSinTotCout
156 1078791
\n" ], "text/latex": [ "\\begin{tabular}{r|ll}\n", " TotalNbSin & TotCout\\\\\n", "\\hline\n", "\t 156 & 1078791\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "TotalNbSin | TotCout | \n", "|---|\n", "| 156 | 1078791 | \n", "\n", "\n" ], "text/plain": [ " TotalNbSin TotCout\n", "1 156 1078791" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "summarise(df_ass,TotalNbSin=sum(nbsin), TotCout= sum((coutTot), na.rm = T))" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Cherchons par exemple nombre de sinistres totaux ainsi que leurs coûts par territoire. En utilisant la syntaxe du _piping_, ça devient plus facile d'inclure plus de sous-groupes;" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\t\n", "\n", "
type_territoireTotalNbSinTotCout
Rural 51 547105.01
Semi-urbain80 471157.64
Urbain 25 60528.81
\n" ], "text/latex": [ "\\begin{tabular}{r|lll}\n", " type\\_territoire & TotalNbSin & TotCout\\\\\n", "\\hline\n", "\t Rural & 51 & 547105.01 \\\\\n", "\t Semi-urbain & 80 & 471157.64 \\\\\n", "\t Urbain & 25 & 60528.81 \\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "type_territoire | TotalNbSin | TotCout | \n", "|---|---|---|\n", "| Rural | 51 | 547105.01 | \n", "| Semi-urbain | 80 | 471157.64 | \n", "| Urbain | 25 | 60528.81 | \n", "\n", "\n" ], "text/plain": [ " type_territoire TotalNbSin TotCout \n", "1 Rural 51 547105.01\n", "2 Semi-urbain 80 471157.64\n", "3 Urbain 25 60528.81" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "df_ass %>% \n", " group_by(type_territoire) %>%\n", " summarise(TotalNbSin=sum(nbsin), \n", " TotCout= sum((coutTot), na.rm = T)\n", " )" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Jointure des bases des données" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "Dans cette section, nous allons joindre deux ou plusieurs df. Mais d'abord importons deux df afin illustrer quelques exemples;" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\n", "
nameprovincecompanylanguedate_naissanceageeage_permisnumeropol
Shane Robinson Nova Scotia May Ltd fr 1944-10-20 72 24 1
Courtney Nguyen Saskatchewan Foley, Moore and Mitchellen 1985-12-09 31 24 5
Lori Washington Yukon Territory Robinson-Reyes fr 1970-01-27 47 28 13
Heidi Freeman Northwest Territories Singh, Esparza and Santosen 1951-06-07 65 18 84
Morgan Buchanan Northwest Territories Rollins Inc fr 1971-07-31 45 31 91
\n" ], "text/latex": [ "\\begin{tabular}{r|llllllll}\n", " name & province & company & langue & date\\_naissance & agee & age\\_permis & numeropol\\\\\n", "\\hline\n", "\t Shane Robinson & Nova Scotia & May Ltd & fr & 1944-10-20 & 72 & 24 & 1 \\\\\n", "\t Courtney Nguyen & Saskatchewan & Foley, Moore and Mitchell & en & 1985-12-09 & 31 & 24 & 5 \\\\\n", "\t Lori Washington & Yukon Territory & Robinson-Reyes & fr & 1970-01-27 & 47 & 28 & 13 \\\\\n", "\t ⋮ & ⋮ & ⋮ & ⋮ & ⋮ & ⋮ & ⋮ & ⋮\\\\\n", "\t Heidi Freeman & Northwest Territories & Singh, Esparza and Santos & en & 1951-06-07 & 65 & 18 & 84 \\\\\n", "\t Morgan Buchanan & Northwest Territories & Rollins Inc & fr & 1971-07-31 & 45 & 31 & 91 \\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "name | province | company | langue | date_naissance | agee | age_permis | numeropol | \n", "|---|---|---|---|---|---|\n", "| Shane Robinson | Nova Scotia | May Ltd | fr | 1944-10-20 | 72 | 24 | 1 | \n", "| Courtney Nguyen | Saskatchewan | Foley, Moore and Mitchell | en | 1985-12-09 | 31 | 24 | 5 | \n", "| Lori Washington | Yukon Territory | Robinson-Reyes | fr | 1970-01-27 | 47 | 28 | 13 | \n", "| ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | \n", "| Heidi Freeman | Northwest Territories | Singh, Esparza and Santos | en | 1951-06-07 | 65 | 18 | 84 | \n", "| Morgan Buchanan | Northwest Territories | Rollins Inc | fr | 1971-07-31 | 45 | 31 | 91 | \n", "\n", "\n" ], "text/plain": [ " name province company langue\n", "1 Shane Robinson Nova Scotia May Ltd fr \n", "2 Courtney Nguyen Saskatchewan Foley, Moore and Mitchell en \n", "3 Lori Washington Yukon Territory Robinson-Reyes fr \n", "⋮ ⋮ ⋮ ⋮ ⋮ \n", "19 Heidi Freeman Northwest Territories Singh, Esparza and Santos en \n", "20 Morgan Buchanan Northwest Territories Rollins Inc fr \n", " date_naissance agee age_permis numeropol\n", "1 1944-10-20 72 24 1 \n", "2 1985-12-09 31 24 5 \n", "3 1970-01-27 47 28 13 \n", "⋮ ⋮ ⋮ ⋮ ⋮ \n", "19 1951-06-07 65 18 84 \n", "20 1971-07-31 45 31 91 " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "df_demo <-read.csv(\"https://raw.githubusercontent.com/nmeraihi/data/master/donnes_demo.csv\", header = T)\n", "df_demo" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\n", "
numeropolmarque_voiturecouleur_voiturepresence_alarmelicense_plate
1 Autres Autre 0 DW 3168
5 RENAULTAutre 0 926 1RL
13 RENAULTAutre 1 SOV 828
84 HONDA Autre 0 CBV 102
91 BMW Autre 1 UOR-0725
\n" ], "text/latex": [ "\\begin{tabular}{r|lllll}\n", " numeropol & marque\\_voiture & couleur\\_voiture & presence\\_alarme & license\\_plate\\\\\n", "\\hline\n", "\t 1 & Autres & Autre & 0 & DW 3168\\\\\n", "\t 5 & RENAULT & Autre & 0 & 926 1RL\\\\\n", "\t 13 & RENAULT & Autre & 1 & SOV 828\\\\\n", "\t ⋮ & ⋮ & ⋮ & ⋮ & ⋮\\\\\n", "\t 84 & HONDA & Autre & 0 & CBV 102 \\\\\n", "\t 91 & BMW & Autre & 1 & UOR-0725\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "numeropol | marque_voiture | couleur_voiture | presence_alarme | license_plate | \n", "|---|---|---|---|---|---|\n", "| 1 | Autres | Autre | 0 | DW 3168 | \n", "| 5 | RENAULT | Autre | 0 | 926 1RL | \n", "| 13 | RENAULT | Autre | 1 | SOV 828 | \n", "| ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | \n", "| 84 | HONDA | Autre | 0 | CBV 102 | \n", "| 91 | BMW | Autre | 1 | UOR-0725 | \n", "\n", "\n" ], "text/plain": [ " numeropol marque_voiture couleur_voiture presence_alarme license_plate\n", "1 1 Autres Autre 0 DW 3168 \n", "2 5 RENAULT Autre 0 926 1RL \n", "3 13 RENAULT Autre 1 SOV 828 \n", "⋮ ⋮ ⋮ ⋮ ⋮ ⋮ \n", "24 84 HONDA Autre 0 CBV 102 \n", "25 91 BMW Autre 1 UOR-0725 " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "df_auto <-read.csv(\"https://raw.githubusercontent.com/nmeraihi/data/master/cars_info.csv\", header = T)\n", "df_auto" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "Dans ces deux df, nous avons une colonne en commun `numeropol`" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/html": [ "
    \n", "\t
  1. 1
  2. \n", "\t
  3. 5
  4. \n", "\t
  5. 13
  6. \n", "\t
  7. 16
  8. \n", "\t
  9. 22
  10. \n", "\t
  11. 28
  12. \n", "\t
  13. 29
  14. \n", "\t
  15. 49
  16. \n", "\t
  17. 53
  18. \n", "\t
  19. 57
  20. \n", "\t
  21. 59
  22. \n", "\t
  23. 65
  24. \n", "\t
  25. 67
  26. \n", "\t
  27. 68
  28. \n", "\t
  29. 69
  30. \n", "\t
  31. 72
  32. \n", "\t
  33. 78
  34. \n", "\t
  35. 83
  36. \n", "\t
  37. 84
  38. \n", "\t
  39. 91
  40. \n", "
\n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item 1\n", "\\item 5\n", "\\item 13\n", "\\item 16\n", "\\item 22\n", "\\item 28\n", "\\item 29\n", "\\item 49\n", "\\item 53\n", "\\item 57\n", "\\item 59\n", "\\item 65\n", "\\item 67\n", "\\item 68\n", "\\item 69\n", "\\item 72\n", "\\item 78\n", "\\item 83\n", "\\item 84\n", "\\item 91\n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. 1\n", "2. 5\n", "3. 13\n", "4. 16\n", "5. 22\n", "6. 28\n", "7. 29\n", "8. 49\n", "9. 53\n", "10. 57\n", "11. 59\n", "12. 65\n", "13. 67\n", "14. 68\n", "15. 69\n", "16. 72\n", "17. 78\n", "18. 83\n", "19. 84\n", "20. 91\n", "\n", "\n" ], "text/plain": [ " [1] 1 5 13 16 22 28 29 49 53 57 59 65 67 68 69 72 78 83 84 91" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "df_demo$numeropol" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/html": [ "
    \n", "\t
  1. 1
  2. \n", "\t
  3. 5
  4. \n", "\t
  5. 13
  6. \n", "\t
  7. 16
  8. \n", "\t
  9. 22
  10. \n", "\t
  11. 22
  12. \n", "\t
  13. 28
  14. \n", "\t
  15. 29
  16. \n", "\t
  17. 49
  18. \n", "\t
  19. 53
  20. \n", "\t
  21. 53
  22. \n", "\t
  23. 57
  24. \n", "\t
  25. 59
  26. \n", "\t
  27. 65
  28. \n", "\t
  29. 65
  30. \n", "\t
  31. 67
  32. \n", "\t
  33. 68
  34. \n", "\t
  35. 69
  36. \n", "\t
  37. 69
  38. \n", "\t
  39. 72
  40. \n", "\t
  41. 78
  42. \n", "\t
  43. 83
  44. \n", "\t
  45. 84
  46. \n", "\t
  47. 84
  48. \n", "\t
  49. 91
  50. \n", "
\n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item 1\n", "\\item 5\n", "\\item 13\n", "\\item 16\n", "\\item 22\n", "\\item 22\n", "\\item 28\n", "\\item 29\n", "\\item 49\n", "\\item 53\n", "\\item 53\n", "\\item 57\n", "\\item 59\n", "\\item 65\n", "\\item 65\n", "\\item 67\n", "\\item 68\n", "\\item 69\n", "\\item 69\n", "\\item 72\n", "\\item 78\n", "\\item 83\n", "\\item 84\n", "\\item 84\n", "\\item 91\n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. 1\n", "2. 5\n", "3. 13\n", "4. 16\n", "5. 22\n", "6. 22\n", "7. 28\n", "8. 29\n", "9. 49\n", "10. 53\n", "11. 53\n", "12. 57\n", "13. 59\n", "14. 65\n", "15. 65\n", "16. 67\n", "17. 68\n", "18. 69\n", "19. 69\n", "20. 72\n", "21. 78\n", "22. 83\n", "23. 84\n", "24. 84\n", "25. 91\n", "\n", "\n" ], "text/plain": [ " [1] 1 5 13 16 22 22 28 29 49 53 53 57 59 65 65 67 68 69 69 72 78 83 84 84 91" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "df_auto$numeropol" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "On peut voir l'index des lignes qui se trouvent dans les deux df " ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/html": [ "
    \n", "\t
  1. 1
  2. \n", "\t
  3. 2
  4. \n", "\t
  5. 3
  6. \n", "\t
  7. 4
  8. \n", "\t
  9. 5
  10. \n", "\t
  11. 7
  12. \n", "\t
  13. 8
  14. \n", "\t
  15. 9
  16. \n", "\t
  17. 10
  18. \n", "\t
  19. 12
  20. \n", "\t
  21. 13
  22. \n", "\t
  23. 14
  24. \n", "\t
  25. 16
  26. \n", "\t
  27. 17
  28. \n", "\t
  29. 18
  30. \n", "\t
  31. 20
  32. \n", "\t
  33. 21
  34. \n", "\t
  35. 22
  36. \n", "\t
  37. 23
  38. \n", "\t
  39. 25
  40. \n", "
\n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item 1\n", "\\item 2\n", "\\item 3\n", "\\item 4\n", "\\item 5\n", "\\item 7\n", "\\item 8\n", "\\item 9\n", "\\item 10\n", "\\item 12\n", "\\item 13\n", "\\item 14\n", "\\item 16\n", "\\item 17\n", "\\item 18\n", "\\item 20\n", "\\item 21\n", "\\item 22\n", "\\item 23\n", "\\item 25\n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. 1\n", "2. 2\n", "3. 3\n", "4. 4\n", "5. 5\n", "6. 7\n", "7. 8\n", "8. 9\n", "9. 10\n", "10. 12\n", "11. 13\n", "12. 14\n", "13. 16\n", "14. 17\n", "15. 18\n", "16. 20\n", "17. 21\n", "18. 22\n", "19. 23\n", "20. 25\n", "\n", "\n" ], "text/plain": [ " [1] 1 2 3 4 5 7 8 9 10 12 13 14 16 17 18 20 21 22 23 25" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "match(df_demo$numeropol, df_auto$numeropol)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/html": [ "
    \n", "\t
  1. 1
  2. \n", "\t
  3. 5
  4. \n", "\t
  5. 13
  6. \n", "\t
  7. 16
  8. \n", "\t
  9. 22
  10. \n", "\t
  11. 29
  12. \n", "\t
  13. 49
  14. \n", "\t
  15. 53
  16. \n", "\t
  17. 57
  18. \n", "\t
  19. 65
  20. \n", "\t
  21. 67
  22. \n", "\t
  23. 68
  24. \n", "\t
  25. 72
  26. \n", "\t
  27. 78
  28. \n", "\t
  29. 83
  30. \n", "\t
  31. 91
  32. \n", "\t
  33. <NA>
  34. \n", "\t
  35. <NA>
  36. \n", "\t
  37. <NA>
  38. \n", "\t
  39. <NA>
  40. \n", "
\n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item 1\n", "\\item 5\n", "\\item 13\n", "\\item 16\n", "\\item 22\n", "\\item 29\n", "\\item 49\n", "\\item 53\n", "\\item 57\n", "\\item 65\n", "\\item 67\n", "\\item 68\n", "\\item 72\n", "\\item 78\n", "\\item 83\n", "\\item 91\n", "\\item \n", "\\item \n", "\\item \n", "\\item \n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. 1\n", "2. 5\n", "3. 13\n", "4. 16\n", "5. 22\n", "6. 29\n", "7. 49\n", "8. 53\n", "9. 57\n", "10. 65\n", "11. 67\n", "12. 68\n", "13. 72\n", "14. 78\n", "15. 83\n", "16. 91\n", "17. <NA>\n", "18. <NA>\n", "19. <NA>\n", "20. <NA>\n", "\n", "\n" ], "text/plain": [ " [1] 1 5 13 16 22 29 49 53 57 65 67 68 72 78 83 91 NA NA NA NA" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "df_demo$numeropol[match(df_demo$numeropol, df_auto$numeropol)]" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "On peut aussi faire un test logique sur la présence des observations du df_demo dans df_auto;" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/html": [ "
    \n", "\t
  1. TRUE
  2. \n", "\t
  3. TRUE
  4. \n", "\t
  5. TRUE
  6. \n", "\t
  7. TRUE
  8. \n", "\t
  9. TRUE
  10. \n", "\t
  11. TRUE
  12. \n", "\t
  13. TRUE
  14. \n", "\t
  15. TRUE
  16. \n", "\t
  17. TRUE
  18. \n", "\t
  19. TRUE
  20. \n", "\t
  21. TRUE
  22. \n", "\t
  23. TRUE
  24. \n", "\t
  25. TRUE
  26. \n", "\t
  27. TRUE
  28. \n", "\t
  29. TRUE
  30. \n", "\t
  31. TRUE
  32. \n", "\t
  33. TRUE
  34. \n", "\t
  35. TRUE
  36. \n", "\t
  37. TRUE
  38. \n", "\t
  39. TRUE
  40. \n", "
\n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item TRUE\n", "\\item TRUE\n", "\\item TRUE\n", "\\item TRUE\n", "\\item TRUE\n", "\\item TRUE\n", "\\item TRUE\n", "\\item TRUE\n", "\\item TRUE\n", "\\item TRUE\n", "\\item TRUE\n", "\\item TRUE\n", "\\item TRUE\n", "\\item TRUE\n", "\\item TRUE\n", "\\item TRUE\n", "\\item TRUE\n", "\\item TRUE\n", "\\item TRUE\n", "\\item TRUE\n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. TRUE\n", "2. TRUE\n", "3. TRUE\n", "4. TRUE\n", "5. TRUE\n", "6. TRUE\n", "7. TRUE\n", "8. TRUE\n", "9. TRUE\n", "10. TRUE\n", "11. TRUE\n", "12. TRUE\n", "13. TRUE\n", "14. TRUE\n", "15. TRUE\n", "16. TRUE\n", "17. TRUE\n", "18. TRUE\n", "19. TRUE\n", "20. TRUE\n", "\n", "\n" ], "text/plain": [ " [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE\n", "[16] TRUE TRUE TRUE TRUE TRUE" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "df_demo$numeropol %in% df_auto$numeropol" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "ou le contraire maintenant" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/html": [ "
    \n", "\t
  1. TRUE
  2. \n", "\t
  3. TRUE
  4. \n", "\t
  5. TRUE
  6. \n", "\t
  7. TRUE
  8. \n", "\t
  9. TRUE
  10. \n", "\t
  11. TRUE
  12. \n", "\t
  13. TRUE
  14. \n", "\t
  15. TRUE
  16. \n", "\t
  17. TRUE
  18. \n", "\t
  19. TRUE
  20. \n", "\t
  21. TRUE
  22. \n", "\t
  23. TRUE
  24. \n", "\t
  25. TRUE
  26. \n", "\t
  27. TRUE
  28. \n", "\t
  29. TRUE
  30. \n", "\t
  31. TRUE
  32. \n", "\t
  33. TRUE
  34. \n", "\t
  35. TRUE
  36. \n", "\t
  37. TRUE
  38. \n", "\t
  39. TRUE
  40. \n", "\t
  41. TRUE
  42. \n", "\t
  43. TRUE
  44. \n", "\t
  45. TRUE
  46. \n", "\t
  47. TRUE
  48. \n", "\t
  49. TRUE
  50. \n", "
\n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item TRUE\n", "\\item TRUE\n", "\\item TRUE\n", "\\item TRUE\n", "\\item TRUE\n", "\\item TRUE\n", "\\item TRUE\n", "\\item TRUE\n", "\\item TRUE\n", "\\item TRUE\n", "\\item TRUE\n", "\\item TRUE\n", "\\item TRUE\n", "\\item TRUE\n", "\\item TRUE\n", "\\item TRUE\n", "\\item TRUE\n", "\\item TRUE\n", "\\item TRUE\n", "\\item TRUE\n", "\\item TRUE\n", "\\item TRUE\n", "\\item TRUE\n", "\\item TRUE\n", "\\item TRUE\n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. TRUE\n", "2. TRUE\n", "3. TRUE\n", "4. TRUE\n", "5. TRUE\n", "6. TRUE\n", "7. TRUE\n", "8. TRUE\n", "9. TRUE\n", "10. TRUE\n", "11. TRUE\n", "12. TRUE\n", "13. TRUE\n", "14. TRUE\n", "15. TRUE\n", "16. TRUE\n", "17. TRUE\n", "18. TRUE\n", "19. TRUE\n", "20. TRUE\n", "21. TRUE\n", "22. TRUE\n", "23. TRUE\n", "24. TRUE\n", "25. TRUE\n", "\n", "\n" ], "text/plain": [ " [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE\n", "[16] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "df_auto$numeropol %in% df_demo$numeropol " ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "Dans ce cas toutes les variables se trouvent dans les deux df" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\n", "
numeropolnameprovincecompanymarque_voiturecouleur_voiturepresence_alarmelicense_plate
1 Shane Robinson Nova Scotia May Ltd Autres Autre 0 DW 3168
5 Courtney Nguyen Saskatchewan Foley, Moore and MitchellRENAULT Autre 0 926 1RL
13 Lori Washington Yukon Territory Robinson-Reyes RENAULT Autre 1 SOV 828
84 Heidi Freeman Northwest Territories Singh, Esparza and SantosHONDA Autre 0 CBV 102
91 Morgan Buchanan Northwest Territories Rollins Inc BMW Autre 1 UOR-0725
\n" ], "text/latex": [ "\\begin{tabular}{r|llllllllllll}\n", " numeropol & name & province & company & ⋯ & marque\\_voiture & couleur\\_voiture & presence\\_alarme & license\\_plate\\\\\n", "\\hline\n", "\t 1 & Shane Robinson & Nova Scotia & May Ltd & ⋯ & Autres & Autre & 0 & DW 3168 \\\\\n", "\t 5 & Courtney Nguyen & Saskatchewan & Foley, Moore and Mitchell & ⋯ & RENAULT & Autre & 0 & 926 1RL \\\\\n", "\t 13 & Lori Washington & Yukon Territory & Robinson-Reyes & ⋯ & RENAULT & Autre & 1 & SOV 828 \\\\\n", "\t ⋮ & ⋮ & ⋮ & ⋮ & ⋱ & ⋮ & ⋮ & ⋮ & ⋮\\\\\n", "\t 84 & Heidi Freeman & Northwest Territories & Singh, Esparza and Santos & ⋯ & HONDA & Autre & 0 & CBV 102 \\\\\n", "\t 91 & Morgan Buchanan & Northwest Territories & Rollins Inc & ⋯ & BMW & Autre & 1 & UOR-0725 \\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "numeropol | name | province | company | ⋯ | marque_voiture | couleur_voiture | presence_alarme | license_plate | \n", "|---|---|---|---|---|---|\n", "| 1 | Shane Robinson | Nova Scotia | May Ltd | ⋯ | Autres | Autre | 0 | DW 3168 | \n", "| 5 | Courtney Nguyen | Saskatchewan | Foley, Moore and Mitchell | ⋯ | RENAULT | Autre | 0 | 926 1RL | \n", "| 13 | Lori Washington | Yukon Territory | Robinson-Reyes | ⋯ | RENAULT | Autre | 1 | SOV 828 | \n", "| ⋮ | ⋮ | ⋮ | ⋮ | ⋱ | ⋮ | ⋮ | ⋮ | ⋮ | \n", "| 84 | Heidi Freeman | Northwest Territories | Singh, Esparza and Santos | ⋯ | HONDA | Autre | 0 | CBV 102 | \n", "| 91 | Morgan Buchanan | Northwest Territories | Rollins Inc | ⋯ | BMW | Autre | 1 | UOR-0725 | \n", "\n", "\n" ], "text/plain": [ " numeropol name province company ⋯\n", "1 1 Shane Robinson Nova Scotia May Ltd ⋯\n", "2 5 Courtney Nguyen Saskatchewan Foley, Moore and Mitchell ⋯\n", "3 13 Lori Washington Yukon Territory Robinson-Reyes ⋯\n", "⋮ ⋮ ⋮ ⋮ ⋮ ⋱\n", "24 84 Heidi Freeman Northwest Territories Singh, Esparza and Santos ⋯\n", "25 91 Morgan Buchanan Northwest Territories Rollins Inc ⋯\n", " marque_voiture couleur_voiture presence_alarme license_plate\n", "1 Autres Autre 0 DW 3168 \n", "2 RENAULT Autre 0 926 1RL \n", "3 RENAULT Autre 1 SOV 828 \n", "⋮ ⋮ ⋮ ⋮ ⋮ \n", "24 HONDA Autre 0 CBV 102 \n", "25 BMW Autre 1 UOR-0725 " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "merge(df_demo,df_auto, by.x = \"numeropol\", by.y = \"numeropol\") # x est le df_demo et y est le df df_auto" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "Que serait-il arrivé si l’on n'avait pas spécifié les arguments `by.x = \"numeropol\", by.y = \"numeropol\"`?" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\n", "
numeropolnameprovincecompanymarque_voiturecouleur_voiturepresence_alarmelicense_plate
1 Shane Robinson Nova Scotia May Ltd Autres Autre 0 DW 3168
5 Courtney Nguyen Saskatchewan Foley, Moore and MitchellRENAULT Autre 0 926 1RL
13 Lori Washington Yukon Territory Robinson-Reyes RENAULT Autre 1 SOV 828
84 Heidi Freeman Northwest Territories Singh, Esparza and SantosHONDA Autre 0 CBV 102
91 Morgan Buchanan Northwest Territories Rollins Inc BMW Autre 1 UOR-0725
\n" ], "text/latex": [ "\\begin{tabular}{r|llllllllllll}\n", " numeropol & name & province & company & ⋯ & marque\\_voiture & couleur\\_voiture & presence\\_alarme & license\\_plate\\\\\n", "\\hline\n", "\t 1 & Shane Robinson & Nova Scotia & May Ltd & ⋯ & Autres & Autre & 0 & DW 3168 \\\\\n", "\t 5 & Courtney Nguyen & Saskatchewan & Foley, Moore and Mitchell & ⋯ & RENAULT & Autre & 0 & 926 1RL \\\\\n", "\t 13 & Lori Washington & Yukon Territory & Robinson-Reyes & ⋯ & RENAULT & Autre & 1 & SOV 828 \\\\\n", "\t ⋮ & ⋮ & ⋮ & ⋮ & ⋱ & ⋮ & ⋮ & ⋮ & ⋮\\\\\n", "\t 84 & Heidi Freeman & Northwest Territories & Singh, Esparza and Santos & ⋯ & HONDA & Autre & 0 & CBV 102 \\\\\n", "\t 91 & Morgan Buchanan & Northwest Territories & Rollins Inc & ⋯ & BMW & Autre & 1 & UOR-0725 \\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "numeropol | name | province | company | ⋯ | marque_voiture | couleur_voiture | presence_alarme | license_plate | \n", "|---|---|---|---|---|---|\n", "| 1 | Shane Robinson | Nova Scotia | May Ltd | ⋯ | Autres | Autre | 0 | DW 3168 | \n", "| 5 | Courtney Nguyen | Saskatchewan | Foley, Moore and Mitchell | ⋯ | RENAULT | Autre | 0 | 926 1RL | \n", "| 13 | Lori Washington | Yukon Territory | Robinson-Reyes | ⋯ | RENAULT | Autre | 1 | SOV 828 | \n", "| ⋮ | ⋮ | ⋮ | ⋮ | ⋱ | ⋮ | ⋮ | ⋮ | ⋮ | \n", "| 84 | Heidi Freeman | Northwest Territories | Singh, Esparza and Santos | ⋯ | HONDA | Autre | 0 | CBV 102 | \n", "| 91 | Morgan Buchanan | Northwest Territories | Rollins Inc | ⋯ | BMW | Autre | 1 | UOR-0725 | \n", "\n", "\n" ], "text/plain": [ " numeropol name province company ⋯\n", "1 1 Shane Robinson Nova Scotia May Ltd ⋯\n", "2 5 Courtney Nguyen Saskatchewan Foley, Moore and Mitchell ⋯\n", "3 13 Lori Washington Yukon Territory Robinson-Reyes ⋯\n", "⋮ ⋮ ⋮ ⋮ ⋮ ⋱\n", "24 84 Heidi Freeman Northwest Territories Singh, Esparza and Santos ⋯\n", "25 91 Morgan Buchanan Northwest Territories Rollins Inc ⋯\n", " marque_voiture couleur_voiture presence_alarme license_plate\n", "1 Autres Autre 0 DW 3168 \n", "2 RENAULT Autre 0 926 1RL \n", "3 RENAULT Autre 1 SOV 828 \n", "⋮ ⋮ ⋮ ⋮ ⋮ \n", "24 HONDA Autre 0 CBV 102 \n", "25 BMW Autre 1 UOR-0725 " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "merge(df_demo,df_auto)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Cela a bien fonctionné, car R a automatiquement trouvé les noms de colonnes communs au deux df;" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "Maintenant, changeons les noms de colonnes et voyons ce qui arrive" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "collapsed": true, "jupyter": { "outputs_hidden": true }, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "names(df_auto)[names(df_auto)==\"numeropol\"] <- \"auto_numpol\"" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Bien évidemment, cela crée une jointure croisée comme on l'avait vu dans les cours de SAS" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\n", "
nameprovincecompanylanguemarque_voiturecouleur_voiturepresence_alarmelicense_plate
Shane Robinson Nova Scotia May Ltd fr Autres Autre 0 DW 3168
Courtney Nguyen Saskatchewan Foley, Moore and Mitchellen Autres Autre 0 DW 3168
Lori Washington Yukon Territory Robinson-Reyes fr Autres Autre 0 DW 3168
Jeffrey Garcia Nunavut Berger-Thompsonen Autres Autre 0 DW 3168
Colleen ColemanSaskatchewan Simmons-Smith en Autres Autre 0 DW 3168
\n" ], "text/latex": [ "\\begin{tabular}{r|lllllllllllll}\n", " name & province & company & langue & ⋯ & marque\\_voiture & couleur\\_voiture & presence\\_alarme & license\\_plate\\\\\n", "\\hline\n", "\t Shane Robinson & Nova Scotia & May Ltd & fr & ⋯ & Autres & Autre & 0 & DW 3168 \\\\\n", "\t Courtney Nguyen & Saskatchewan & Foley, Moore and Mitchell & en & ⋯ & Autres & Autre & 0 & DW 3168 \\\\\n", "\t Lori Washington & Yukon Territory & Robinson-Reyes & fr & ⋯ & Autres & Autre & 0 & DW 3168 \\\\\n", "\t ⋮ & ⋮ & ⋮ & ⋮ & ⋱ & ⋮ & ⋮ & ⋮ & ⋮\\\\\n", "\t Jeffrey Garcia & Nunavut & Berger-Thompson & en & ⋯ & Autres & Autre & 0 & DW 3168 \\\\\n", "\t Colleen Coleman & Saskatchewan & Simmons-Smith & en & ⋯ & Autres & Autre & 0 & DW 3168 \\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "name | province | company | langue | ⋯ | marque_voiture | couleur_voiture | presence_alarme | license_plate | \n", "|---|---|---|---|---|---|\n", "| Shane Robinson | Nova Scotia | May Ltd | fr | ⋯ | Autres | Autre | 0 | DW 3168 | \n", "| Courtney Nguyen | Saskatchewan | Foley, Moore and Mitchell | en | ⋯ | Autres | Autre | 0 | DW 3168 | \n", "| Lori Washington | Yukon Territory | Robinson-Reyes | fr | ⋯ | Autres | Autre | 0 | DW 3168 | \n", "| ⋮ | ⋮ | ⋮ | ⋮ | ⋱ | ⋮ | ⋮ | ⋮ | ⋮ | \n", "| Jeffrey Garcia | Nunavut | Berger-Thompson | en | ⋯ | Autres | Autre | 0 | DW 3168 | \n", "| Colleen Coleman | Saskatchewan | Simmons-Smith | en | ⋯ | Autres | Autre | 0 | DW 3168 | \n", "\n", "\n" ], "text/plain": [ " name province company langue ⋯\n", "1 Shane Robinson Nova Scotia May Ltd fr ⋯\n", "2 Courtney Nguyen Saskatchewan Foley, Moore and Mitchell en ⋯\n", "3 Lori Washington Yukon Territory Robinson-Reyes fr ⋯\n", "⋮ ⋮ ⋮ ⋮ ⋮ ⋱\n", "5 Jeffrey Garcia Nunavut Berger-Thompson en ⋯\n", "6 Colleen Coleman Saskatchewan Simmons-Smith en ⋯\n", " marque_voiture couleur_voiture presence_alarme license_plate\n", "1 Autres Autre 0 DW 3168 \n", "2 Autres Autre 0 DW 3168 \n", "3 Autres Autre 0 DW 3168 \n", "⋮ ⋮ ⋮ ⋮ ⋮ \n", "5 Autres Autre 0 DW 3168 \n", "6 Autres Autre 0 DW 3168 " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "head(merge(df_demo,df_auto))" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "On vient bien que dans la dernière colonne `license_plate`, nous obtenons la même observation ce qui est clairement une erreur;" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Corrigeons le problème;" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\n", "
numeropolnameprovincecompanymarque_voiturecouleur_voiturepresence_alarmelicense_plate
1 Shane Robinson Nova Scotia May Ltd Autres Autre 0 DW 3168
5 Courtney Nguyen Saskatchewan Foley, Moore and MitchellRENAULT Autre 0 926 1RL
13 Lori Washington Yukon Territory Robinson-Reyes RENAULT Autre 1 SOV 828
22 Jeffrey Garcia Nunavut Berger-ThompsonVOLKSWAGEN Autre 1 453 CFM
22 Jeffrey Garcia Nunavut Berger-ThompsonVOLKSWAGEN Autre 0 FHH 537
\n" ], "text/latex": [ "\\begin{tabular}{r|llllllllllll}\n", " numeropol & name & province & company & ⋯ & marque\\_voiture & couleur\\_voiture & presence\\_alarme & license\\_plate\\\\\n", "\\hline\n", "\t 1 & Shane Robinson & Nova Scotia & May Ltd & ⋯ & Autres & Autre & 0 & DW 3168 \\\\\n", "\t 5 & Courtney Nguyen & Saskatchewan & Foley, Moore and Mitchell & ⋯ & RENAULT & Autre & 0 & 926 1RL \\\\\n", "\t 13 & Lori Washington & Yukon Territory & Robinson-Reyes & ⋯ & RENAULT & Autre & 1 & SOV 828 \\\\\n", "\t ⋮ & ⋮ & ⋮ & ⋮ & ⋱ & ⋮ & ⋮ & ⋮ & ⋮\\\\\n", "\t 22 & Jeffrey Garcia & Nunavut & Berger-Thompson & ⋯ & VOLKSWAGEN & Autre & 1 & 453 CFM \\\\\n", "\t 22 & Jeffrey Garcia & Nunavut & Berger-Thompson & ⋯ & VOLKSWAGEN & Autre & 0 & FHH 537 \\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "numeropol | name | province | company | ⋯ | marque_voiture | couleur_voiture | presence_alarme | license_plate | \n", "|---|---|---|---|---|---|\n", "| 1 | Shane Robinson | Nova Scotia | May Ltd | ⋯ | Autres | Autre | 0 | DW 3168 | \n", "| 5 | Courtney Nguyen | Saskatchewan | Foley, Moore and Mitchell | ⋯ | RENAULT | Autre | 0 | 926 1RL | \n", "| 13 | Lori Washington | Yukon Territory | Robinson-Reyes | ⋯ | RENAULT | Autre | 1 | SOV 828 | \n", "| ⋮ | ⋮ | ⋮ | ⋮ | ⋱ | ⋮ | ⋮ | ⋮ | ⋮ | \n", "| 22 | Jeffrey Garcia | Nunavut | Berger-Thompson | ⋯ | VOLKSWAGEN | Autre | 1 | 453 CFM | \n", "| 22 | Jeffrey Garcia | Nunavut | Berger-Thompson | ⋯ | VOLKSWAGEN | Autre | 0 | FHH 537 | \n", "\n", "\n" ], "text/plain": [ " numeropol name province company ⋯\n", "1 1 Shane Robinson Nova Scotia May Ltd ⋯\n", "2 5 Courtney Nguyen Saskatchewan Foley, Moore and Mitchell ⋯\n", "3 13 Lori Washington Yukon Territory Robinson-Reyes ⋯\n", "⋮ ⋮ ⋮ ⋮ ⋮ ⋱\n", "5 22 Jeffrey Garcia Nunavut Berger-Thompson ⋯\n", "6 22 Jeffrey Garcia Nunavut Berger-Thompson ⋯\n", " marque_voiture couleur_voiture presence_alarme license_plate\n", "1 Autres Autre 0 DW 3168 \n", "2 RENAULT Autre 0 926 1RL \n", "3 RENAULT Autre 1 SOV 828 \n", "⋮ ⋮ ⋮ ⋮ ⋮ \n", "5 VOLKSWAGEN Autre 1 453 CFM \n", "6 VOLKSWAGEN Autre 0 FHH 537 " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "head(merge(df_demo,df_auto, by.x = \"numeropol\", by.y = \"auto_numpol\"))" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ " ## left_join" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "la fonction `left_join` prend toute l'information de gauche et l'information existante de la partie droite qui est basée sur le critère en commun" ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\n", "
nombureaux
Gabriel5518
Adel 4538
NM 5518
Amine 4538
Mohamed4540
\n" ], "text/latex": [ "\\begin{tabular}{r|ll}\n", " nom & bureaux\\\\\n", "\\hline\n", "\t Gabriel & 5518 \\\\\n", "\t Adel & 4538 \\\\\n", "\t NM & 5518 \\\\\n", "\t ⋮ & ⋮\\\\\n", "\t Amine & 4538 \\\\\n", "\t Mohamed & 4540 \\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "nom | bureaux | \n", "|---|---|---|---|---|---|\n", "| Gabriel | 5518 | \n", "| Adel | 4538 | \n", "| NM | 5518 | \n", "| ⋮ | ⋮ | \n", "| Amine | 4538 | \n", "| Mohamed | 4540 | \n", "\n", "\n" ], "text/plain": [ " nom bureaux\n", "1 Gabriel 5518 \n", "2 Adel 4538 \n", "3 NM 5518 \n", "⋮ ⋮ ⋮ \n", "5 Amine 4538 \n", "6 Mohamed 4540 " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "x<-data.frame(nom=c(\"Gabriel\", \"Adel\", \"NM\", \"Mathieu\", \"Amine\", \"Mohamed\"), \n", " bureaux=c(\"5518\", \"4538\", \"5518\", \"5517\", \"4538\", \"4540\"))\n", "x" ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\n", "
nomdiplome
GabrielM.Sc
Adel Ph.D
JP Ph.D
MathieuPh.D
Amine Ph.D
\n" ], "text/latex": [ "\\begin{tabular}{r|ll}\n", " nom & diplome\\\\\n", "\\hline\n", "\t Gabriel & M.Sc \\\\\n", "\t Adel & Ph.D \\\\\n", "\t JP & Ph.D \\\\\n", "\t Mathieu & Ph.D \\\\\n", "\t Amine & Ph.D \\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "nom | diplome | \n", "|---|---|---|---|---|\n", "| Gabriel | M.Sc | \n", "| Adel | Ph.D | \n", "| JP | Ph.D | \n", "| Mathieu | Ph.D | \n", "| Amine | Ph.D | \n", "\n", "\n" ], "text/plain": [ " nom diplome\n", "1 Gabriel M.Sc \n", "2 Adel Ph.D \n", "3 JP Ph.D \n", "4 Mathieu Ph.D \n", "5 Amine Ph.D " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "y<-data.frame(nom=c(\"Gabriel\", \"Adel\", \"JP\", \"Mathieu\", \"Amine\"), \n", " diplome=c(\"M.Sc\", \"Ph.D\", \"Ph.D\", \"Ph.D\", \"Ph.D\"))\n", "y" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Warning message:\n", "“Column `nom` joining factors with different levels, coercing to character vector”" ] }, { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\n", "
nombureauxdiplome
Gabriel5518 M.Sc
Adel 4538 Ph.D
NM 5518 NA
Amine 4538 Ph.D
Mohamed4540 NA
\n" ], "text/latex": [ "\\begin{tabular}{r|lll}\n", " nom & bureaux & diplome\\\\\n", "\\hline\n", "\t Gabriel & 5518 & M.Sc \\\\\n", "\t Adel & 4538 & Ph.D \\\\\n", "\t NM & 5518 & NA \\\\\n", "\t ⋮ & ⋮ & ⋮\\\\\n", "\t Amine & 4538 & Ph.D \\\\\n", "\t Mohamed & 4540 & NA \\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "nom | bureaux | diplome | \n", "|---|---|---|---|---|---|\n", "| Gabriel | 5518 | M.Sc | \n", "| Adel | 4538 | Ph.D | \n", "| NM | 5518 | NA | \n", "| ⋮ | ⋮ | ⋮ | \n", "| Amine | 4538 | Ph.D | \n", "| Mohamed | 4540 | NA | \n", "\n", "\n" ], "text/plain": [ " nom bureaux diplome\n", "1 Gabriel 5518 M.Sc \n", "2 Adel 4538 Ph.D \n", "3 NM 5518 NA \n", "⋮ ⋮ ⋮ ⋮ \n", "5 Amine 4538 Ph.D \n", "6 Mohamed 4540 NA " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "left_join(x,y,by = \"nom\")" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## inner_join" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "Cette fonction permet de retourner **seulement** les éléments en commun des deux df" ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Warning message:\n", "“Column `nom` joining factors with different levels, coercing to character vector”" ] }, { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\t\n", "\t\n", "\n", "
nombureauxdiplome
Gabriel5518 M.Sc
Adel 4538 Ph.D
Mathieu5517 Ph.D
Amine 4538 Ph.D
\n" ], "text/latex": [ "\\begin{tabular}{r|lll}\n", " nom & bureaux & diplome\\\\\n", "\\hline\n", "\t Gabriel & 5518 & M.Sc \\\\\n", "\t Adel & 4538 & Ph.D \\\\\n", "\t Mathieu & 5517 & Ph.D \\\\\n", "\t Amine & 4538 & Ph.D \\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "nom | bureaux | diplome | \n", "|---|---|---|---|\n", "| Gabriel | 5518 | M.Sc | \n", "| Adel | 4538 | Ph.D | \n", "| Mathieu | 5517 | Ph.D | \n", "| Amine | 4538 | Ph.D | \n", "\n", "\n" ], "text/plain": [ " nom bureaux diplome\n", "1 Gabriel 5518 M.Sc \n", "2 Adel 4538 Ph.D \n", "3 Mathieu 5517 Ph.D \n", "4 Amine 4538 Ph.D " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "inner_join(x,y,by = \"nom\")" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## semi_join" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Cette fonction retourne seulement les éléments du premier df qui se retrouve dans le deuxième df, sans nous retourner les éléments de ce dernier" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "jupyter": { "outputs_hidden": true }, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "semi_join(x,y,by = \"nom\")" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## anti_join" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Cette fonction le contraire de la précédente" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "jupyter": { "outputs_hidden": true }, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "anti_join(x,y,by = \"nom\")" ] } ], "metadata": { "anaconda-cloud": {}, "celltoolbar": "Slideshow", "kernelspec": { "display_name": "R", "language": "R", "name": "ir" }, "language_info": { "codemirror_mode": "r", "file_extension": ".r", "mimetype": "text/x-r-source", "name": "R", "pygments_lexer": "r", "version": "4.1.2" }, "latex_envs": { "LaTeX_envs_menu_present": true, "autoclose": false, "autocomplete": true, "bibliofile": "biblio.bib", "cite_by": "apalike", "current_citInitial": 1, "eqLabelWithNumbers": true, "eqNumInitial": 1, "hotkeys": { "equation": "Ctrl-E", "itemize": "Ctrl-I" }, "labels_anchors": false, "latex_user_defs": false, "report_style_numbering": false, "user_envs_cfg": false }, "toc": { "base_numbering": 1, "nav_menu": { "height": "444px", "width": "252px" }, "number_sections": false, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": true, "toc_position": { "height": "1132px", "left": "0px", "right": "20px", "top": "159px", "width": "211px" }, "toc_section_display": "block", "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 4 }