Vous avez reçu un message "Your GitLab account has been locked ..." ? Pas d'inquiétude : lisez cet article https://docs.gricad-pages.univ-grenoble-alpes.fr/help/unlock/

ManipDon.ipynb 63.1 KB
Newer Older
1
2
{
 "cells": [
Laurence Viry's avatar
Laurence Viry committed
3
4
  {
   "cell_type": "code",
Laurence Viry's avatar
Laurence Viry committed
5
   "execution_count": null,
Laurence Viry's avatar
Laurence Viry committed
6
7
8
9
10
11
12
13
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "setwd(\"/home/viryl/notebooks/ATMO_IntroR\")"
   ]
  },
14
15
16
17
18
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Manipuler des données dans R\n",
19
20
21
22
23
24
25
26
27
28
    "\n",
    "En statistique, les données constituent le point de départ de toute analyse, un premier travail de mise en forme de ces données est presque toujours indispensable. Il faudra savoir maitriser des opérations comme:\n",
    "\n",
    "* Importation de données sous différents formats, \n",
    "* exporter des données et des résultats sous différents formats,\n",
    "* concaténer ou extraire des données, \n",
    "* repérer les individus ayant des données manquantes ou aberrantes.\n",
    "* changer le type de certaines variables pour les adapter aux traitements envisagés,\n",
    "* $\\ldots$\n",
    "\n",
Laurence Viry's avatar
Laurence Viry committed
29
30
    "On abordera le concept de *tidy data$, les extensions du tidyverse comme *dplyr* ou *ggplot2* partent du principe que les données sont “bien rangées” sous forme de tidy data.\n",
    "\n",
31
32
33
34
    "R fournit des outils et des capacités de programmation pour effectuer ces différentes tâches.\n",
    "\n",
    "## Importer des données\n",
    "\n",
Laurence Viry's avatar
Laurence Viry committed
35
    "Les données sont initialement collectées, stockées sous différents formats, éventuellement prétraitées par un logiciel ou extraites d'une base de données. \n",
36
    "\n",
Laurence Viry's avatar
Laurence Viry committed
37
    "Chaque logiciel ayant son propre format de stockage, le plus simple est souvent d'échanger les données par un format commun à tous qui sera le plus souvent **le format texte** ( .csv par exemple).\n",
Laurence Viry's avatar
Laurence Viry committed
38
    "\n",
Laurence Viry's avatar
Laurence Viry committed
39
    "On peut également utiliser **les formats propriétaires** (SAS,SPSS,...) des autres logiciels en utilisant un package adapté (le package **foreign** par exemple), le choix dépendant du contexte et du volume des données.\n",
Laurence Viry's avatar
Laurence Viry committed
40
    "\n",
Laurence Viry's avatar
Laurence Viry committed
41
    "## Importer des données en format texte\n",
Laurence Viry's avatar
Laurence Viry committed
42
    "### Cas des fichiers **csv** \n",
Laurence Viry's avatar
Laurence Viry committed
43
44
45
    "\n",
    "Les avantages des fichiers **csv**:\n",
    "\n",
Laurence Viry's avatar
Laurence Viry committed
46
47
    "  - Peut être lu par n'importe quel logiciel passé, présent et probablement futur.\n",
    "  - Pour la compatibilité entre plate-forme (Windows, Mac, Linux).\n",
Laurence Viry's avatar
Laurence Viry committed
48
49
50
51
52
53
54
55
56
57
58
59
    "  - Pour la facilité de lecture par un être humain comparativement à d'autres formats tels que XML, HL7, JSON etc.\n",
    "\n",
    "**Mais** pas forcément adapté aux gros volumes de données pour son volume de stockage et la rapidité de lecture.\n",
    "\n",
    "R lit des données en format texte avec les fonctions **read.table()**,**read.csv()**,**scan()**,**read.fwf()**,$\\ldots$\n",
    "\n",
    "* Le fichier \"donnees.csv\" est stocké dans le répertoire data sitée dans le répertoire de travail \n",
    "    \n",
    "    - pour connaître le répertoire de travail, utiliser la fonction **getwd()**\n",
    "    - pour définir le répertoire de travail, la fonction **setwd()**\n",
    "    \n",
    " Le résultat de la fonction **read.table** ou **read.csv** est de type **data-frame**."
60
61
62
63
   ]
  },
  {
   "cell_type": "code",
Laurence Viry's avatar
Laurence Viry committed
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Lecture du fichier donnees.csv\n",
    "getwd() # repertoire de travail\n",
    "don <- read.csv(file = \"data/donnees.csv\",header=TRUE,sep=\";\",dec=\",\",row.names=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "   - l'argument **sep** : indique que les valeurs sont séparées par **\";\"** (**\" \"** pour un espace, **\"\\t\"** pour une tabulation,$\\ldots$)\n",
    "   - l'argument **dec** : indique que le séparateur de décimal est **\",\"**\n",
    "   - l'argument **header** : indique si la première ligne contient les noms des variables (TRUE) ou non(FALSE).\n",
    "   - l'argument **row.names** : indique que la colonne 1 n'est pas une variable mais l'identifiant des individus.\n",
    "   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
Laurence Viry's avatar
Laurence Viry committed
89
90
   "metadata": {},
   "outputs": [
Laurence Viry's avatar
Laurence Viry committed
91
92
93
94
    {
     "data": {
      "text/html": [
       "'list'"
Laurence Viry's avatar
Laurence Viry committed
95
96
      ],
      "text/latex": [
Laurence Viry's avatar
Laurence Viry committed
97
       "'list'"
Laurence Viry's avatar
Laurence Viry committed
98
99
      ],
      "text/markdown": [
Laurence Viry's avatar
Laurence Viry committed
100
       "'list'"
Laurence Viry's avatar
Laurence Viry committed
101
102
      ],
      "text/plain": [
Laurence Viry's avatar
Laurence Viry committed
103
       "[1] \"list\""
Laurence Viry's avatar
Laurence Viry committed
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "'data.frame'"
      ],
      "text/latex": [
       "'data.frame'"
      ],
      "text/markdown": [
       "'data.frame'"
      ],
      "text/plain": [
       "[1] \"data.frame\""
      ]
     },
     "metadata": {},
     "output_type": "display_data"
Laurence Viry's avatar
Laurence Viry committed
126
127
128
129
130
131
132
133
134
135
136
137
138
    }
   ],
   "source": [
    "# \"mode\" et \"class\" \n",
    "mode(don)\n",
    "class(don)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "metadata": {},
   "outputs": [
Laurence Viry's avatar
Laurence Viry committed
139
140
141
142
    {
     "data": {
      "text/plain": [
       "     taille          poids          pointure    sexe \n",
Laurence Viry's avatar
Laurence Viry committed
143
144
145
146
147
       " Min.   :158.0   Min.   : 5.00   Min.   :42.0   M:3  \n",
       " 1st Qu.:166.8   1st Qu.:38.50   1st Qu.:42.5        \n",
       " Median :175.5   Median :72.00   Median :43.0        \n",
       " Mean   :172.5   Mean   :52.33   Mean   :43.0        \n",
       " 3rd Qu.:179.8   3rd Qu.:76.00   3rd Qu.:43.5        \n",
Laurence Viry's avatar
Laurence Viry committed
148
149
150
151
152
       " Max.   :184.0   Max.   :80.00   Max.   :44.0        "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
Laurence Viry's avatar
Laurence Viry committed
153
154
    }
   ],
Laurence Viry's avatar
Laurence Viry committed
155
   "source": [
Laurence Viry's avatar
Laurence Viry committed
156
    "# Statistiques des variables du data.frame\n",
Laurence Viry's avatar
Laurence Viry committed
157
    "summary(don)"
Laurence Viry's avatar
Laurence Viry committed
158
159
   ]
  },
Laurence Viry's avatar
Laurence Viry committed
160
161
162
163
164
165
166
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Fonctions utilisees sur un data-frame"
   ]
  },
Laurence Viry's avatar
Laurence Viry committed
167
168
  {
   "cell_type": "code",
Laurence Viry's avatar
Laurence Viry committed
169
   "execution_count": 109,
Laurence Viry's avatar
Laurence Viry committed
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<dl>\n",
       "\t<dt>$names</dt>\n",
       "\t\t<dd><ol class=list-inline>\n",
       "\t<li>'taille'</li>\n",
       "\t<li>'poids'</li>\n",
       "\t<li>'pointure'</li>\n",
       "\t<li>'sexe'</li>\n",
       "</ol>\n",
       "</dd>\n",
       "\t<dt>$class</dt>\n",
       "\t\t<dd>'data.frame'</dd>\n",
       "\t<dt>$row.names</dt>\n",
       "\t\t<dd><ol class=list-inline>\n",
       "\t<li>'roger'</li>\n",
       "\t<li>'theodule'</li>\n",
       "\t<li>'nicolas'</li>\n",
       "</ol>\n",
       "</dd>\n",
       "</dl>\n"
      ],
      "text/latex": [
       "\\begin{description}\n",
       "\\item[\\$names] \\begin{enumerate*}\n",
       "\\item 'taille'\n",
       "\\item 'poids'\n",
       "\\item 'pointure'\n",
       "\\item 'sexe'\n",
       "\\end{enumerate*}\n",
       "\n",
       "\\item[\\$class] 'data.frame'\n",
       "\\item[\\$row.names] \\begin{enumerate*}\n",
       "\\item 'roger'\n",
       "\\item 'theodule'\n",
       "\\item 'nicolas'\n",
       "\\end{enumerate*}\n",
       "\n",
       "\\end{description}\n"
      ],
      "text/markdown": [
       "$names\n",
       ":   1. 'taille'\n",
       "2. 'poids'\n",
       "3. 'pointure'\n",
       "4. 'sexe'\n",
       "\n",
       "\n",
       "\n",
       "$class\n",
       ":   'data.frame'\n",
       "$row.names\n",
       ":   1. 'roger'\n",
       "2. 'theodule'\n",
       "3. 'nicolas'\n",
       "\n",
       "\n",
       "\n",
       "\n",
       "\n"
      ],
      "text/plain": [
       "$names\n",
       "[1] \"taille\"   \"poids\"    \"pointure\" \"sexe\"    \n",
       "\n",
       "$class\n",
       "[1] \"data.frame\"\n",
       "\n",
       "$row.names\n",
Laurence Viry's avatar
Laurence Viry committed
242
       "[1] \"roger\"    \"theodule\" \"nicolas\" \n"
Laurence Viry's avatar
Laurence Viry committed
243
244
245
246
      ]
     },
     "metadata": {},
     "output_type": "display_data"
Laurence Viry's avatar
Laurence Viry committed
247
248
249
250
251
252
253
254
255
256
257
258
    }
   ],
   "source": [
    "# Attributs d'un data-frame\n",
    "attributes(don)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "metadata": {},
   "outputs": [
Laurence Viry's avatar
Laurence Viry committed
259
260
261
262
263
264
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "'data.frame':\t3 obs. of  4 variables:\n",
      " $ taille  : num  184 176 158\n",
Laurence Viry's avatar
Laurence Viry committed
265
      " $ poids   : int  80 5 72\n",
Laurence Viry's avatar
Laurence Viry committed
266
267
268
      " $ pointure: int  44 43 42\n",
      " $ sexe    : Factor w/ 1 level \"M\": 1 1 1\n"
     ]
Laurence Viry's avatar
Laurence Viry committed
269
270
271
272
273
274
275
276
277
278
279
280
    }
   ],
   "source": [
    "# Afficher de manière compacte la structure d'un objet R \n",
    "str(don)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "metadata": {},
   "outputs": [
Laurence Viry's avatar
Laurence Viry committed
281
282
    {
     "data": {
Laurence Viry's avatar
Laurence Viry committed
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
      "text/html": [
       "<ol class=list-inline>\n",
       "\t<li>'taille'</li>\n",
       "\t<li>'poids'</li>\n",
       "\t<li>'pointure'</li>\n",
       "\t<li>'sexe'</li>\n",
       "</ol>\n"
      ],
      "text/latex": [
       "\\begin{enumerate*}\n",
       "\\item 'taille'\n",
       "\\item 'poids'\n",
       "\\item 'pointure'\n",
       "\\item 'sexe'\n",
       "\\end{enumerate*}\n"
      ],
      "text/markdown": [
       "1. 'taille'\n",
       "2. 'poids'\n",
       "3. 'pointure'\n",
       "4. 'sexe'\n",
       "\n",
       "\n"
      ],
Laurence Viry's avatar
Laurence Viry committed
307
      "text/plain": [
Laurence Viry's avatar
Laurence Viry committed
308
       "[1] \"taille\"   \"poids\"    \"pointure\" \"sexe\"    "
Laurence Viry's avatar
Laurence Viry committed
309
310
311
312
      ]
     },
     "metadata": {},
     "output_type": "display_data"
Laurence Viry's avatar
Laurence Viry committed
313
314
315
316
317
318
319
320
321
322
323
324
    }
   ],
   "source": [
    "# Nom des variables \n",
    "names(don)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "metadata": {},
   "outputs": [
Laurence Viry's avatar
Laurence Viry committed
325
326
327
    {
     "data": {
      "text/html": [
Laurence Viry's avatar
Laurence Viry committed
328
       "3"
Laurence Viry's avatar
Laurence Viry committed
329
330
      ],
      "text/latex": [
Laurence Viry's avatar
Laurence Viry committed
331
       "3"
Laurence Viry's avatar
Laurence Viry committed
332
333
      ],
      "text/markdown": [
Laurence Viry's avatar
Laurence Viry committed
334
       "3"
Laurence Viry's avatar
Laurence Viry committed
335
336
      ],
      "text/plain": [
Laurence Viry's avatar
Laurence Viry committed
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
       "[1] 3"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "4"
      ],
      "text/latex": [
       "4"
      ],
      "text/markdown": [
       "4"
      ],
      "text/plain": [
       "[1] 4"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
Laurence Viry's avatar
Laurence Viry committed
360
361
362
363
364
365
366
367
368
369
370
371
372
    }
   ],
   "source": [
    "# Nombre de lignes(individus) et de colonnes(variables)\n",
    "nrow(don)\n",
    "ncol(don)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "metadata": {},
   "outputs": [
Laurence Viry's avatar
Laurence Viry committed
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
    {
     "data": {
      "text/html": [
       "<ol>\n",
       "\t<li><ol class=list-inline>\n",
       "\t<li>'roger'</li>\n",
       "\t<li>'theodule'</li>\n",
       "\t<li>'nicolas'</li>\n",
       "</ol>\n",
       "</li>\n",
       "\t<li><ol class=list-inline>\n",
       "\t<li>'taille'</li>\n",
       "\t<li>'poids'</li>\n",
       "\t<li>'pointure'</li>\n",
       "\t<li>'sexe'</li>\n",
       "</ol>\n",
       "</li>\n",
       "</ol>\n"
      ],
      "text/latex": [
       "\\begin{enumerate}\n",
       "\\item \\begin{enumerate*}\n",
       "\\item 'roger'\n",
       "\\item 'theodule'\n",
       "\\item 'nicolas'\n",
       "\\end{enumerate*}\n",
       "\n",
       "\\item \\begin{enumerate*}\n",
       "\\item 'taille'\n",
       "\\item 'poids'\n",
       "\\item 'pointure'\n",
       "\\item 'sexe'\n",
       "\\end{enumerate*}\n",
       "\n",
       "\\end{enumerate}\n"
      ],
      "text/markdown": [
       "1. 1. 'roger'\n",
       "2. 'theodule'\n",
       "3. 'nicolas'\n",
       "\n",
       "\n",
       "\n",
       "2. 1. 'taille'\n",
       "2. 'poids'\n",
       "3. 'pointure'\n",
       "4. 'sexe'\n",
       "\n",
       "\n",
       "\n",
       "\n",
       "\n"
      ],
      "text/plain": [
       "[[1]]\n",
       "[1] \"roger\"    \"theodule\" \"nicolas\" \n",
       "\n",
       "[[2]]\n",
       "[1] \"taille\"   \"poids\"    \"pointure\" \"sexe\"    \n"
Laurence Viry's avatar
Laurence Viry committed
432
433
434
435
436
437
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
Laurence Viry's avatar
Laurence Viry committed
438
   "source": [
Laurence Viry's avatar
Laurence Viry committed
439
    "# Nom des lignes(1.) et des colonnes(2.)\n",
Laurence Viry's avatar
Laurence Viry committed
440
    "dimnames(don)"
Laurence Viry's avatar
Laurence Viry committed
441
442
443
444
445
446
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
Laurence Viry's avatar
Laurence Viry committed
447
    "#### Un caractère spécial peut indiquer qu'il y a des données manquantes:\n",
Laurence Viry's avatar
Laurence Viry committed
448
449
    "\n",
    "Le fichier **don2.csv** contient des données manquantes codées **\"\\*\\*\\*\"**, on ajoute l'argument **na.strings**"
Laurence Viry's avatar
Laurence Viry committed
450
451
452
453
   ]
  },
  {
   "cell_type": "code",
Laurence Viry's avatar
Laurence Viry committed
454
   "execution_count": 118,
Laurence Viry's avatar
Laurence Viry committed
455
456
457
458
459
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
Laurence Viry's avatar
Laurence Viry committed
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
       "     taille          poids       pointure       sexe  \n",
       " Min.   :100.0   Min.   :15   Min.   :22.00   F   :2  \n",
       " 1st Qu.:110.0   1st Qu.:30   1st Qu.:30.50   M   :3  \n",
       " Median :160.0   Median :72   Median :40.00   NA's:1  \n",
       " Mean   :145.9   Mean   :55   Mean   :36.17           \n",
       " 3rd Qu.:175.5   3rd Qu.:78   3rd Qu.:42.75           \n",
       " Max.   :184.0   Max.   :80   Max.   :44.00           \n",
       " NA's   :1       NA's   :1                            "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "[1] NA"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "55"
      ],
      "text/latex": [
       "55"
      ],
      "text/markdown": [
       "55"
      ],
      "text/plain": [
       "[1] 55"
Laurence Viry's avatar
Laurence Viry committed
495
496
497
498
499
500
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
Laurence Viry's avatar
Laurence Viry committed
501
   "source": [
Laurence Viry's avatar
Laurence Viry committed
502
503
504
505
    "don2 <- read.csv(file = \"data/don2.csv\",header=TRUE,sep=\";\",dec=\",\",row.names=1,na.strings=\"***\")\n",
    "summary(don2)\n",
    "mean(don2$poids)\n",
    "mean(don2$poids,na.rm=TRUE)"
Laurence Viry's avatar
Laurence Viry committed
506
507
508
509
510
511
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
Laurence Viry's avatar
Laurence Viry committed
512
    "#### Le chemin peut-être une URL:"
Laurence Viry's avatar
Laurence Viry committed
513
514
515
516
   ]
  },
  {
   "cell_type": "code",
Laurence Viry's avatar
Laurence Viry committed
517
   "execution_count": 119,
Laurence Viry's avatar
Laurence Viry committed
518
   "metadata": {},
Laurence Viry's avatar
Laurence Viry committed
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
   "outputs": [
    {
     "data": {
      "text/plain": [
       "     SCORE            TIME    DECIMAL.TIME            CLASS  \n",
       " Min.   : 6.0   03:22:50:1   Min.   :0.140   BEST        :2  \n",
       " 1st Qu.: 8.0   07:42:51:1   1st Qu.:0.320   INTERMEDIATE:1  \n",
       " Median :13.0   09:30:03:1   Median :0.400   WORST       :2  \n",
       " Mean   :12.4   12:01:03:1   Mean   :0.372                   \n",
       " 3rd Qu.:16.0   12:01:29:1   3rd Qu.:0.500                   \n",
       " Max.   :19.0                Max.   :0.500                   "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
Laurence Viry's avatar
Laurence Viry committed
536
   "source": [
Laurence Viry's avatar
Laurence Viry committed
537
538
    "df <- read.table(\"https://s3.amazonaws.com/assets.datacamp.com/blog_assets/scores_timed.csv\",header=TRUE,row.names = 1,sep = \",\")\n",
    "summary(df)"
Laurence Viry's avatar
Laurence Viry committed
539
540
541
542
543
544
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
Laurence Viry's avatar
Laurence Viry committed
545
546
547
548
549
550
551
552
553
    "### La fonction **scan**\n",
    "\n",
    "La fonction **scan** est plus flexible que **read.table**.\n",
    "\n",
    "* Une différence est qu'il est possible de spécifier le mode des variables:"
   ]
  },
  {
   "cell_type": "code",
Laurence Viry's avatar
Laurence Viry committed
554
   "execution_count": 120,
Laurence Viry's avatar
Laurence Viry committed
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "'list'"
      ],
      "text/latex": [
       "'list'"
      ],
      "text/markdown": [
       "'list'"
      ],
      "text/plain": [
       "[1] \"list\""
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "List of 5\n",
      " $ : chr [1:3] \"roger\" \"theodule\" \"nicolas\"\n",
      " $ : num [1:3] 184 176 158\n",
      " $ : num [1:3] 80 5 72\n",
      " $ : num [1:3] 44 43 42\n",
      " $ : chr [1:3] \"M\" \"M\" \"M\"\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<ol class=list-inline>\n",
       "\t<li>'roger'</li>\n",
       "\t<li>'theodule'</li>\n",
       "\t<li>'nicolas'</li>\n",
       "</ol>\n"
      ],
      "text/latex": [
       "\\begin{enumerate*}\n",
       "\\item 'roger'\n",
       "\\item 'theodule'\n",
       "\\item 'nicolas'\n",
       "\\end{enumerate*}\n"
      ],
      "text/markdown": [
       "1. 'roger'\n",
       "2. 'theodule'\n",
       "3. 'nicolas'\n",
       "\n",
       "\n"
      ],
      "text/plain": [
       "[1] \"roger\"    \"theodule\" \"nicolas\" "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "'roger'"
      ],
      "text/latex": [
       "'roger'"
      ],
      "text/markdown": [
       "'roger'"
      ],
      "text/plain": [
       "[1] \"roger\""
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
636
   "source": [
Laurence Viry's avatar
Laurence Viry committed
637
    "mydata <- scan(\"data/donnees.csv\",skip=1,sep=\";\",dec=\",\",what = list(\"\", 0, 0,0,\"\"))\n",
Laurence Viry's avatar
Laurence Viry committed
638
    "class(mydata)\n",
Laurence Viry's avatar
Laurence Viry committed
639
    "str(mydata)\n",
Laurence Viry's avatar
Laurence Viry committed
640
641
    "mydata[[1]] # premiere variable\n",
    "mydata[[1]][1] # "
Laurence Viry's avatar
Laurence Viry committed
642
643
644
645
646
647
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
Laurence Viry's avatar
Laurence Viry committed
648
    "Dans cet exemple, **scan** lit *5 variables*, la première en mode caractère, les trois suivantes sont en mode numérique et la cinquième en mode caractère.\n",
Laurence Viry's avatar
Laurence Viry committed
649
    "\n",
Laurence Viry's avatar
Laurence Viry committed
650
    "**mydata** est une liste de 5 vecteurs.\n",
Laurence Viry's avatar
Laurence Viry committed
651
652
653
654
655
656
    "\n",
    "* scan() peut être utilisée pour créer des objets de mode différent (vecteurs, matrices, tableaux de données, listes,...).\n",
    "*\n",
    "* Par défaut, c'est-à-dire si what est omis, scan() crée un vecteur numérique.\n",
    "\n",
    "Pour en savoir plus **help(scan)**"
657
658
   ]
  },
Laurence Viry's avatar
Laurence Viry committed
659
660
661
662
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
Laurence Viry's avatar
Laurence Viry committed
663
664
    "## Importer des fichiers Excel\n",
    "Le package **readxl** est l'outil le plus simple pour importer des fichiers *Excel* au format **xls** ou **xlsx**, il n'a pas de dépendances externes et il est facile à installer sur tout système. Il existe d'autres packages comme *gdata, xlsx, xlsReadWrite*...\n",
665
    "\n",
Laurence Viry's avatar
Laurence Viry committed
666
    "Le package **readxl** fait partie du package **tidyverse** mais il peut être installé séparément et devra être chargé séparément.\n",
667
    "\n",
Laurence Viry's avatar
Laurence Viry committed
668
    "On utilisera le fichier Excel \"datasets.xls\" (ou \"datasets.xlsx\") fournit par le package *readxl*.\n",
Laurence Viry's avatar
Laurence Viry committed
669
    "\n",
Laurence Viry's avatar
Laurence Viry committed
670
671
    "### Lire le fichier excel:\n",
    "On utilise la fonction **read_excel**, l'objet fournit est de type \"*data.frame*,*tbl_df*,*tbl*\". La fonction lit à la fois les fichiers de type **xls et xlsx**, il détermine le format à partir de l'extension."
Laurence Viry's avatar
Laurence Viry committed
672
673
674
   ]
  },
  {
Laurence Viry's avatar
Laurence Viry committed
675
676
677
678
679
680
   "cell_type": "code",
   "execution_count": 121,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
Laurence Viry's avatar
Laurence Viry committed
681
   "source": [
Laurence Viry's avatar
Laurence Viry committed
682
683
    "# Importer le package\n",
    "library(\"readxl\")"
Laurence Viry's avatar
Laurence Viry committed
684
685
686
   ]
  },
  {
Laurence Viry's avatar
Laurence Viry committed
687
688
   "cell_type": "code",
   "execution_count": 124,
Laurence Viry's avatar
Laurence Viry committed
689
   "metadata": {},
Laurence Viry's avatar
Laurence Viry committed
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
   "outputs": [
    {
     "data": {
      "text/html": [
       "'/opt/conda/lib/R/library/readxl/extdata/datasets.xls'"
      ],
      "text/latex": [
       "'/opt/conda/lib/R/library/readxl/extdata/datasets.xls'"
      ],
      "text/markdown": [
       "'/opt/conda/lib/R/library/readxl/extdata/datasets.xls'"
      ],
      "text/plain": [
       "[1] \"/opt/conda/lib/R/library/readxl/extdata/datasets.xls\""
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
Laurence Viry's avatar
Laurence Viry committed
710
   "source": [
Laurence Viry's avatar
Laurence Viry committed
711
712
713
714
715
    "# Path du fichier excel \"datasets.xls\"\n",
    "datasets <- readxl_example(\"datasets.xls\") # chemin de stockage du fichier excel \"datasets.xls\"\n",
    "datasets\n",
    "# Lecture du fichier excel \"datasets.xls\"\n",
    "df <- read_excel(datasets)"
Laurence Viry's avatar
Laurence Viry committed
716
717
718
719
720
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
Laurence Viry's avatar
Laurence Viry committed
721
722
723
   "metadata": {
    "collapsed": true
   },
Laurence Viry's avatar
Laurence Viry committed
724
   "outputs": [],
Laurence Viry's avatar
Laurence Viry committed
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
   "source": [
    "# \"class\" de l'objet df\n",
    "class(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<ol class=list-inline>\n",
       "\t<li>'tbl_df'</li>\n",
       "\t<li>'tbl'</li>\n",
       "\t<li>'data.frame'</li>\n",
       "</ol>\n"
      ],
      "text/latex": [
       "\\begin{enumerate*}\n",
       "\\item 'tbl\\_df'\n",
       "\\item 'tbl'\n",
       "\\item 'data.frame'\n",
       "\\end{enumerate*}\n"
      ],
      "text/markdown": [
       "1. 'tbl_df'\n",
       "2. 'tbl'\n",
       "3. 'data.frame'\n",
       "\n",
       "\n"
      ],
      "text/plain": [
       "[1] \"tbl_df\"     \"tbl\"        \"data.frame\""
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "  Sepal.Length    Sepal.Width     Petal.Length    Petal.Width   \n",
       " Min.   :4.300   Min.   :2.000   Min.   :1.000   Min.   :0.100  \n",
       " 1st Qu.:5.100   1st Qu.:2.800   1st Qu.:1.600   1st Qu.:0.300  \n",
       " Median :5.800   Median :3.000   Median :4.350   Median :1.300  \n",
       " Mean   :5.843   Mean   :3.057   Mean   :3.758   Mean   :1.199  \n",
       " 3rd Qu.:6.400   3rd Qu.:3.300   3rd Qu.:5.100   3rd Qu.:1.800  \n",
       " Max.   :7.900   Max.   :4.400   Max.   :6.900   Max.   :2.500  \n",
       "   Species         \n",
       " Length:150        \n",
       " Class :character  \n",
       " Mode  :character  \n",
       "                   \n",
       "                   \n",
       "                   "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "summary(df)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Il est possible de spécifier la feuille et la plage de cellules que l’on souhaite importer avec les arguments **sheet** et **range**.\n",
    "### Gestion des feuilles d'un classeur Excel\n",
    "Un **classeur Excel** peut contenir plusieurs feuilles. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 126,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<ol class=list-inline>\n",
       "\t<li>'iris'</li>\n",
       "\t<li>'mtcars'</li>\n",
       "\t<li>'chickwts'</li>\n",
       "\t<li>'quakes'</li>\n",
       "</ol>\n"
      ],
      "text/latex": [
       "\\begin{enumerate*}\n",
       "\\item 'iris'\n",
       "\\item 'mtcars'\n",
       "\\item 'chickwts'\n",
       "\\item 'quakes'\n",
       "\\end{enumerate*}\n"
      ],
      "text/markdown": [
       "1. 'iris'\n",
       "2. 'mtcars'\n",
       "3. 'chickwts'\n",
       "4. 'quakes'\n",
       "\n",
       "\n"
      ],
      "text/plain": [
       "[1] \"iris\"     \"mtcars\"   \"chickwts\" \"quakes\"  "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Liste des feuilles du classeur \"datasets.xls\".\n",
    "excel_sheets(datasets)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 127,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<ol class=list-inline>\n",
       "\t<li>'tbl_df'</li>\n",
       "\t<li>'tbl'</li>\n",
       "\t<li>'data.frame'</li>\n",
       "</ol>\n"
      ],
      "text/latex": [
       "\\begin{enumerate*}\n",
       "\\item 'tbl\\_df'\n",
       "\\item 'tbl'\n",
       "\\item 'data.frame'\n",
       "\\end{enumerate*}\n"
      ],
      "text/markdown": [
       "1. 'tbl_df'\n",
       "2. 'tbl'\n",
       "3. 'data.frame'\n",
       "\n",
       "\n"
      ],
      "text/plain": [
       "[1] \"tbl_df\"     \"tbl\"        \"data.frame\""
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "  Sepal.Length    Sepal.Width     Petal.Length    Petal.Width   \n",
       " Min.   :4.300   Min.   :2.000   Min.   :1.000   Min.   :0.100  \n",
       " 1st Qu.:5.100   1st Qu.:2.800   1st Qu.:1.600   1st Qu.:0.300  \n",
       " Median :5.800   Median :3.000   Median :4.350   Median :1.300  \n",
       " Mean   :5.843   Mean   :3.057   Mean   :3.758   Mean   :1.199  \n",
       " 3rd Qu.:6.400   3rd Qu.:3.300   3rd Qu.:5.100   3rd Qu.:1.800  \n",
       " Max.   :7.900   Max.   :4.400   Max.   :6.900   Max.   :2.500  \n",
       "   Species         \n",
       " Length:150        \n",
       " Class :character  \n",
       " Mode  :character  \n",
       "                   \n",
       "                   \n",
       "                   "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Lecture d'une des feuilles\n",
    "df <- read_excel(datasets,sheet = \"iris\")\n",
    "class(df)\n",
    "summary(df)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*Remarque*: par défaut la fonction read_excel lit la première feuille du fichier Excel fournit en argument.<br>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Plusieurs façons de spécifier quelles cellules sont lues.\n",
    "#### Une partie des lignes du tableau de données"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 128,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table>\n",
       "<thead><tr><th scope=col>Sepal.Length</th><th scope=col>Sepal.Width</th><th scope=col>Petal.Length</th><th scope=col>Petal.Width</th><th scope=col>Species</th></tr></thead>\n",
       "<tbody>\n",
       "\t<tr><td>5.1   </td><td>3.5   </td><td>1.4   </td><td>0.2   </td><td>setosa</td></tr>\n",
       "\t<tr><td>4.9   </td><td>3.0   </td><td>1.4   </td><td>0.2   </td><td>setosa</td></tr>\n",
       "\t<tr><td>4.7   </td><td>3.2   </td><td>1.3   </td><td>0.2   </td><td>setosa</td></tr>\n",
       "</tbody>\n",
       "</table>\n"
      ],
      "text/latex": [
       "\\begin{tabular}{r|lllll}\n",
       " Sepal.Length & Sepal.Width & Petal.Length & Petal.Width & Species\\\\\n",
       "\\hline\n",
       "\t 5.1    & 3.5    & 1.4    & 0.2    & setosa\\\\\n",
       "\t 4.9    & 3.0    & 1.4    & 0.2    & setosa\\\\\n",
       "\t 4.7    & 3.2    & 1.3    & 0.2    & setosa\\\\\n",
       "\\end{tabular}\n"
      ],
      "text/markdown": [
       "\n",
       "Sepal.Length | Sepal.Width | Petal.Length | Petal.Width | Species | \n",
       "|---|---|---|\n",
       "| 5.1    | 3.5    | 1.4    | 0.2    | setosa | \n",
       "| 4.9    | 3.0    | 1.4    | 0.2    | setosa | \n",
       "| 4.7    | 3.2    | 1.3    | 0.2    | setosa | \n",
       "\n",
       "\n"
      ],
      "text/plain": [
       "  Sepal.Length Sepal.Width Petal.Length Petal.Width Species\n",
       "1 5.1          3.5         1.4          0.2         setosa \n",
       "2 4.9          3.0         1.4          0.2         setosa \n",
       "3 4.7          3.2         1.3          0.2         setosa "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "read_excel(datasets,sheet = \"iris\",n_max=3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 130,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table>\n",
       "<thead><tr><th scope=col>Sepal.Length</th><th scope=col>Sepal.Width</th><th scope=col>Petal.Length</th><th scope=col>Petal.Width</th><th scope=col>Species</th></tr></thead>\n",
       "<tbody>\n",
       "\t<tr><td>5.1   </td><td>3.5   </td><td>1.4   </td><td>0.2   </td><td>setosa</td></tr>\n",
       "\t<tr><td>4.9   </td><td>3.0   </td><td>1.4   </td><td>0.2   </td><td>setosa</td></tr>\n",
       "\t<tr><td>4.7   </td><td>3.2   </td><td>1.3   </td><td>0.2   </td><td>setosa</td></tr>\n",
       "</tbody>\n",
       "</table>\n"
      ],
      "text/latex": [
       "\\begin{tabular}{r|lllll}\n",
       " Sepal.Length & Sepal.Width & Petal.Length & Petal.Width & Species\\\\\n",
       "\\hline\n",
       "\t 5.1    & 3.5    & 1.4    & 0.2    & setosa\\\\\n",
       "\t 4.9    & 3.0    & 1.4    & 0.2    & setosa\\\\\n",
       "\t 4.7    & 3.2    & 1.3    & 0.2    & setosa\\\\\n",
       "\\end{tabular}\n"
      ],
      "text/markdown": [
       "\n",
       "Sepal.Length | Sepal.Width | Petal.Length | Petal.Width | Species | \n",
       "|---|---|---|\n",
       "| 5.1    | 3.5    | 1.4    | 0.2    | setosa | \n",
       "| 4.9    | 3.0    | 1.4    | 0.2    | setosa | \n",
       "| 4.7    | 3.2    | 1.3    | 0.2    | setosa | \n",
       "\n",
       "\n"
      ],
      "text/plain": [
       "  Sepal.Length Sepal.Width Petal.Length Petal.Width Species\n",
       "1 5.1          3.5         1.4          0.2         setosa \n",
       "2 4.9          3.0         1.4          0.2         setosa \n",
       "3 4.7          3.2         1.3          0.2         setosa "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "read_excel(datasets,sheet = \"iris\",range = cell_rows(1:4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 131,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table>\n",
       "<thead><tr><th scope=col>Sepal.Length</th><th scope=col>Sepal.Width</th><th scope=col>Petal.Length</th><th scope=col>Petal.Width</th><th scope=col>Species</th></tr></thead>\n",
       "<tbody>\n",
       "\t<tr><td>5.1   </td><td>3.5   </td><td>1.4   </td><td>0.2   </td><td>setosa</td></tr>\n",
       "\t<tr><td>4.9   </td><td>3.0   </td><td>1.4   </td><td>0.2   </td><td>setosa</td></tr>\n",
       "</tbody>\n",
       "</table>\n"
      ],
      "text/latex": [
       "\\begin{tabular}{r|lllll}\n",
       " Sepal.Length & Sepal.Width & Petal.Length & Petal.Width & Species\\\\\n",
       "\\hline\n",
       "\t 5.1    & 3.5    & 1.4    & 0.2    & setosa\\\\\n",
       "\t 4.9    & 3.0    & 1.4    & 0.2    & setosa\\\\\n",
       "\\end{tabular}\n"
      ],
      "text/markdown": [
       "\n",
       "Sepal.Length | Sepal.Width | Petal.Length | Petal.Width | Species | \n",
       "|---|---|\n",
       "| 5.1    | 3.5    | 1.4    | 0.2    | setosa | \n",
       "| 4.9    | 3.0    | 1.4    | 0.2    | setosa | \n",
       "\n",
       "\n"
      ],
      "text/plain": [
       "  Sepal.Length Sepal.Width Petal.Length Petal.Width Species\n",
       "1 5.1          3.5         1.4          0.2         setosa \n",
       "2 4.9          3.0         1.4          0.2         setosa "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "read_excel(datasets,sheet = \"iris\",range = cell_rows(1:3))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Une partie des colonnes"
   ]
  },
  {
   "cell_type": "code",
Laurence Viry's avatar
Laurence Viry committed
1083
   "execution_count": null,
Laurence Viry's avatar
Laurence Viry committed
1084
   "metadata": {},
Laurence Viry's avatar
Laurence Viry committed
1085
   "outputs": [],
Laurence Viry's avatar
Laurence Viry committed
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
   "source": [
    "read_excel(datasets,sheet = \"iris\",range = cell_cols(\"B:D\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**ou**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "read_excel(datasets,sheet = \"iris\",range = cell_cols(2:4))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Une partie des lignes et une partie des colonnes "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 138,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table>\n",
       "<thead><tr><th scope=col>cyl</th><th scope=col>disp</th><th scope=col>hp</th></tr></thead>\n",
       "<tbody>\n",
       "\t<tr><td>6  </td><td>160</td><td>110</td></tr>\n",
       "\t<tr><td>6  </td><td>160</td><td>110</td></tr>\n",
       "\t<tr><td>4  </td><td>108</td><td> 93</td></tr>\n",
       "\t<tr><td>6  </td><td>258</td><td>110</td></tr>\n",
       "</tbody>\n",
       "</table>\n"
      ],
      "text/latex": [
       "\\begin{tabular}{r|lll}\n",
       " cyl & disp & hp\\\\\n",
       "\\hline\n",
       "\t 6   & 160 & 110\\\\\n",
       "\t 6   & 160 & 110\\\\\n",
       "\t 4   & 108 &  93\\\\\n",
       "\t 6   & 258 & 110\\\\\n",
       "\\end{tabular}\n"
      ],
      "text/markdown": [
       "\n",
       "cyl | disp | hp | \n",
       "|---|---|---|---|\n",
       "| 6   | 160 | 110 | \n",
       "| 6   | 160 | 110 | \n",
       "| 4   | 108 |  93 | \n",
       "| 6   | 258 | 110 | \n",
       "\n",
       "\n"
      ],
      "text/plain": [
       "  cyl disp hp \n",
       "1 6   160  110\n",
       "2 6   160  110\n",
       "3 4   108   93\n",
       "4 6   258  110"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "read_excel(datasets,sheet = \"mtcars\",range = \"B1:D5\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**ou**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 137,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table>\n",
       "<thead><tr><th scope=col>cyl</th><th scope=col>disp</th><th scope=col>hp</th></tr></thead>\n",
       "<tbody>\n",
       "\t<tr><td>6  </td><td>160</td><td>110</td></tr>\n",
       "\t<tr><td>6  </td><td>160</td><td>110</td></tr>\n",
       "\t<tr><td>4  </td><td>108</td><td> 93</td></tr>\n",
       "\t<tr><td>6  </td><td>258</td><td>110</td></tr>\n",
       "</tbody>\n",
       "</table>\n"
      ],
      "text/latex": [
       "\\begin{tabular}{r|lll}\n",
       " cyl & disp & hp\\\\\n",
       "\\hline\n",
       "\t 6   & 160 & 110\\\\\n",
       "\t 6   & 160 & 110\\\\\n",
       "\t 4   & 108 &  93\\\\\n",
       "\t 6   & 258 & 110\\\\\n",
       "\\end{tabular}\n"
      ],
      "text/markdown": [
       "\n",
       "cyl | disp | hp | \n",
       "|---|---|---|---|\n",
       "| 6   | 160 | 110 | \n",
       "| 6   | 160 | 110 | \n",
       "| 4   | 108 |  93 | \n",
       "| 6   | 258 | 110 | \n",
       "\n",
       "\n"
      ],
      "text/plain": [
       "  cyl disp hp \n",
       "1 6   160  110\n",
       "2 6   160  110\n",
       "3 4   108   93\n",
       "4 6   258  110"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "read_excel(datasets,range =\"mtcars!B1:D5\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### \"Not Available\" / Missing Values représenté par une chaîne de caractères"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 141,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "df <-read_excel(datasets,sheet = \"iris\",range=\"B1:E5\",na=\"1.4\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 144,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "  Sepal.Width     Petal.Length   Petal.Width    Species         \n",
       " Min.   :3.000   Min.   :1.30   Min.   :0.2   Length:4          \n",
       " 1st Qu.:3.075   1st Qu.:1.35   1st Qu.:0.2   Class :character  \n",
       " Median :3.150   Median :1.40   Median :0.2   Mode  :character  \n",
       " Mean   :3.200   Mean   :1.40   Mean   :0.2                     \n",
       " 3rd Qu.:3.275   3rd Qu.:1.45   3rd Qu.:0.2                     \n",
       " Max.   :3.500   Max.   :1.50   Max.   :0.2                     \n",
       "                 NA's   :2                                      "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "[1] NA"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "1.4"
      ],
      "text/latex": [
       "1.4"
      ],
      "text/markdown": [
       "1.4"
      ],
      "text/plain": [
       "[1] 1.4"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "summary(df)\n",
    "mean(df$Petal.Length)\n",
    "mean(df$Petal.Length,na.rm=TRUE)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Quelques références\n",
    "[Article \"readxl\"](https://readxl.tidyverse.org/articles/index.html) <br>\n",
    "https://readxl.tidyverse.org/<br>\n",
    "[Cookbook for R](http://www.cookbook-r.com/Manipulating_data/)<br>\n",
    "..."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Importer des fichiers avec un format propriétaire\n",
    "\n",
    "R peut également lire des fichiers dans d'autres formats (**Excel, SAS, SPSS**,$\\ldots$) et accéder à des **bases de données**.\n",
    "\n",
    "* Le package **foreign** permet d'importer des données en format propriétaire binaire tels que **Stata, SAS, SPSS, etc**.\n",
    "\n",
    "  **library**(foreign) <br\\>\n",
    "  **read.dta**(\"calf pneu.dta\") # for Stata files<br\\>\n",
    "  **read.xport**(\"file.xpt\") # for SAS XPORT format<br\\>\n",
    "  **read.spss**(\"file.sav\") # for SPSS format<br\\>\n",
    "  **read.mpt**(\"file.mtp\") # for Minitab Portable Worksheet<br\\>\n",
    "\n",
    "Une autre solution pour des **fichiers SPSS**, le package **Hmisc**<br\\>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Base de données relationnelles et autres formats\n",
    "\n",
    "Certains packages permettent de se connecter directement sur des bases de données de type **MySQL** ou **PostgreSQL**\n",
    "(MongoDB,Redis,ousqlite,...). Dans ce cas,le mode d’interaction avec les données est légèrement différent\n",
    " car on utilise alors le langage de requête propre au langage, à moins d’utiliser des packages qui permettent d’assurer\n",
    " la conversion à partir des commandes R habituelles telles que **subset()**.\n",
    "\n",
    "Pour en savoir plus : \n",
    "\n",
    "* [Cookbook for R](http://www.cookbook-r.com/Manipulating_data/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Sauvegarder des données ou des résultats\n",
    "\n",
    "Une fois les analyses effectuées et les résultats obtenus, il est souvent important de les sauvegarder pour les communiquer à d'autres personnes ou d'autres logiciels ou les réutiliser dans d'autres analyses.\n",
    "\n",
    "### En format texte\n",
    "\n",
    "Le format texte est utilisé, il sera compatible avec d'autres logiciels, on utilise la fonction **write.table()**."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 140,
   "metadata": {},
   "outputs": [
    {
     "ename": "ERROR",
     "evalue": "Error in setwd(\"/home/viryl/notebooks/ATMO-IntroR/resultat\"): cannot change working directory\n",
     "output_type": "error",
     "traceback": [
      "Error in setwd(\"/home/viryl/notebooks/ATMO-IntroR/resultat\"): cannot change working directory\nTraceback:\n",
      "1. setwd(\"/home/viryl/notebooks/ATMO-IntroR/resultat\")"
     ]
    }
   ],
Laurence Viry's avatar
Laurence Viry committed
1379
1380
1381
   "source": [
    "tablo_res <- matrix(runif(40), ncol=4)\n",
    "# sauvegarde les resulatts dans le repertoire resultat\n",
Laurence Viry's avatar
Laurence Viry committed
1382
    "setwd(\"/home/viryl/notebooks/ATMO-IntroR/resultat\")\n",
Laurence Viry's avatar
Laurence Viry committed
1383
    "write.table(tablo_res,\"monfichier.csv\",sep=\";\",row.names=FALSE)\n",
Laurence Viry's avatar
Laurence Viry committed
1384
    "\n"
Laurence Viry's avatar
Laurence Viry committed
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### En format binaire RData\n",
    "\n",
    "* Exporter les résultats et/ou les données dans un fichier binaire suffixé\n",
    "**.Rdata** que R sera capable de décrypter par la suite grâce à la fonction **save()**\n",
    "\n",
    "   **save(file=\"nom du fichier\",liste des variables)** (help(save))\n",
    "   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
Laurence Viry's avatar
Laurence Viry committed
1403
1404
1405
   "metadata": {
    "collapsed": true
   },
Laurence Viry's avatar
Laurence Viry committed
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
   "outputs": [],
   "source": [
    "x <- runif(20)\n",
    "y <- list(a = 1, b = TRUE, c = \"oops\")\n",
    "getwd()\n",
    "save(x, y, file = \"xy.RData\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* L'utilisation des fichiers sauvegardées par la commande **save()** se fait par la commande **load()**."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
Laurence Viry's avatar
Laurence Viry committed
1424
1425
1426
   "metadata": {
    "collapsed": true
   },
Laurence Viry's avatar
Laurence Viry committed
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
   "outputs": [],
   "source": [
    "rm(list=ls())\n",
    "\"avant load\"\n",
    "ls()\n",
    "load(file=\"xy.RData\")\n",
    "\"après load\"\n",
    "ls()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
Laurence Viry's avatar
Laurence Viry committed
1441
1442
    "## Manipuler des variables\n",
    "\n",
Laurence Viry's avatar
Laurence Viry committed
1443
1444
1445
1446
1447
    "* **Changer le type d'une variable**: à l'issue d'une importation, une variable qualitative dont les modalitées sont numériques sera comprise par R comme une variable quantitative,$\\ldots$\n",
    "\n",
    "* **Découper en classes une variable quantitative**: le passage d'une variable quantitative à une variable qualitative est fréquemment nécessaire en statistiques pour l'adapter à la méthode utilisée (AFC,AFCM,$\\ldots$)\n",
    "\n",
    "* **Modifier les niveaux d'une variable qualitative**: fusionner un ou plusieurs niveaux en fonction des effectifs,$\\ldots$\n",
Laurence Viry's avatar
Laurence Viry committed
1448
    "\n",
Laurence Viry's avatar
Laurence Viry committed
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
    "* **Repérer les données manquantes**: permettre la prise en compte des données manquantes dans le traitement statistique des données.\n",
    "\n",
    "* **Repérer les données aberrantes**: permettre la prise en compte des données aberrantes dans le traitement statistique des données.\n",
    "\n",
    "* $\\ldots$\n",
    "\n",
    "### Quantitatives ou qualitative?\n",
    "\n",
    " Trois méthodes:\n",
    " "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
Laurence Viry's avatar
Laurence Viry committed
1464
1465
1466
   "metadata": {
    "collapsed": true
   },
Laurence Viry's avatar
Laurence Viry committed
1467
1468
1469
1470
1471
1472
   "outputs": [],
   "source": [
    "# on genere une variable X\n",
    "X <-c(rep(5,2),rep(12,4),13)\n",
    "X\n",
    "# Une variable qualitative?\n",
Laurence Viry's avatar
Laurence Viry committed
1473
    "Xq <is.factor(X)\n",
Laurence Viry's avatar
Laurence Viry committed
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
    "# Une variable quantitative\n",
    "is.numeric(X)\n",
    "# Statistques descriptives\n",
    "summary(X)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Les trois instructions nous indiquent que X est une variable quantitative."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Variable qualitative vers variable quantitative\n",
    "* Avec recodification des modalités: 1 seule étape\n",
    "\n",
    "Par défaut, la conversion d'un facteur en variable quantitative, utilise la recodification des modalités à **1 to nlevels(x)**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
Laurence Viry's avatar
Laurence Viry committed
1500
1501
1502
   "metadata": {
    "collapsed": true
   },
Laurence Viry's avatar
Laurence Viry committed
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
   "outputs": [],
   "source": [
    "Xqual <- factor(c(rep(5,2),seq(0,6,by=2),13))\n",
    "Xqual\n",
    "nlevels(Xqual)\n",
    "summary(Xqual)\n",
    "is.factor(Xqual)\n",
    "as.numeric(Xqual)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* Sans recodification des modalités\n",
    "\n",
    "Si on veut éviter la recofication, lorsque la valeur des modalités a un sens de quantité, il sera nécessaire de faire une première conversion de mode en mode **\"character\"**, avant la conversion en **\"numeric\"**."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
Laurence Viry's avatar
Laurence Viry committed
1525
1526
1527
   "metadata": {
    "collapsed": true
   },
Laurence Viry's avatar
Laurence Viry committed
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
   "outputs": [],
   "source": [
    "Xqual\n",
    "prov <- as.character(Xqual)\n",
    "prov\n",
    "Xquant <- as.numeric(prov) # as.numeric(as.character(Xqual))\n",
    "Xquant\n",
    "summary(Xquant)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
Laurence Viry's avatar
Laurence Viry committed
1542
    "### Variable quantitative vers variable qualitative\n",
Laurence Viry's avatar
Laurence Viry committed
1543
1544
1545
1546
1547
1548
    "* En conservant les valeurs de la variable quantitative"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
Laurence Viry's avatar
Laurence Viry committed
1549
1550
1551
   "metadata": {
    "collapsed": true
   },
Laurence Viry's avatar
Laurence Viry committed
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
   "outputs": [],
   "source": [
    "rm(list=ls())\n",
    "ls()\n",
    "X <- seq(0,10,2)\n",
    "X\n",
    "summary(X)\n",
    "Xqual <- as.factor(X)\n",
    "Xqual\n",
    "summary(Xqual)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Découpage en classes d'une variable quantitative\n",
    "\n",
    "Le découpage en classes d'une variable quantitatives peut se faire avec **deux approches**:\n",
Laurence Viry's avatar
Laurence Viry committed
1571
    "\n",
Laurence Viry's avatar
Laurence Viry committed
1572
1573
1574
1575
1576
1577
    "* Les **seuils des classes** sont choisis par les utilisateurs: pour définir ces seuils de façon automatique, on utilisera la fonction **cut**. Les bornes des intervals sont fournies à la fonction cut, les classes sont de la forme $\\bf{] a_i,a_{i+1}]}$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
Laurence Viry's avatar
Laurence Viry committed
1578
1579
1580
   "metadata": {
    "collapsed": true
   },
Laurence Viry's avatar
Laurence Viry committed
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
   "outputs": [],
   "source": [
    "set.seed(654) # on fixe la graine du generateur\n",
    "X <- rnorm(n=100,mean=0,sd=1)\n",
    "# Decoupage en 3 niveaux: [min(X),-0.2],[-0.2,0.2],[0.2,max(X)]\n",
    "Xqual <- cut(X,breaks = c(min(X)-1e-10,-0.2,0.2,max(X)))\n",
    "class(Xqual)\n",
    "table(Xqual)\n",
    "summary(Xqual) # ou table(Xqual)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "On indique \"$min(X)-1e-10$\" pour que le minimum appartienne à la classe.\n",
Laurence Viry's avatar
Laurence Viry committed
1597
    "\n",
Laurence Viry's avatar
Laurence Viry committed
1598
1599
1600
1601
1602
1603
    "* Un découpage automatique proposant **des effectifs équivalents dans chaque classes**: si nous voulons des effectifs équilibrés dans chacune des trois modalités, on utilisera la **fonction quantile**."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
Laurence Viry's avatar
Laurence Viry committed
1604
1605
1606
   "metadata": {
    "collapsed": true
   },
Laurence Viry's avatar
Laurence Viry committed
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
   "outputs": [],
   "source": [
    "decoupe <- quantile(X,probs=seq(0,1,length=4)) \n",
    "decoupe\n",
    "decoupe[1] <- decoupe[1]-1e-10\n",
    "Xqual <- cut(X,decoupe)\n",
    "table(Xqual)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Modifier les modalités d'un facteur\n",
    "\n",
    "* Modifier les labels des modalités"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
Laurence Viry's avatar
Laurence Viry committed
1628
1629
1630
   "metadata": {
    "collapsed": true
   },
Laurence Viry's avatar
Laurence Viry committed
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
   "outputs": [],
   "source": [
    "Xqual <- cut(X,decoupe)\n",
    "levels(Xqual)\n",
    "#Xqual\n",
    "table(Xqual)\n",
    "#\n",
    "levels(Xqual) <- c(1,2,3) # modification les labels des modalités\n",
    "#Xqual\n",
    "table(Xqual)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* Fusionner un ou plusieurs niveaux"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
Laurence Viry's avatar
Laurence Viry committed
1653
1654
1655
   "metadata": {
    "collapsed": true
   },
Laurence Viry's avatar
Laurence Viry committed
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
   "outputs": [],
   "source": [
    "levels(Xqual)\n",
    "levels(Xqual) <- c(1,2,1)\n",
    "levels(Xqual)\n",
    "table(Xqual)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* Définir un niveau de référence: pour certaines méthodes, il faudra tenir compte de l'ordre d'apparition des niveaux ou spécifier une niveau de référence (analyse de variance,$\\ldots$), on utilise la fonction **relevel**:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
Laurence Viry's avatar
Laurence Viry committed
1674
1675
1676
   "metadata": {
    "collapsed": true
   },
Laurence Viry's avatar
Laurence Viry committed
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
   "outputs": [],
   "source": [
    "X <- c(1,2,1,3,2,2,1)\n",
    "Xqual <- factor(X,label=c(\"classique\",\"nouveau\",\"placebo\"))\n",
    "levels(Xqual)\n",
    "Xqual2 <- relevel(Xqual,ref=\"placebo\")\n",
    "levels(Xqual2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* Contrôler l'ordre des niveaux: recréer un facteur à partir du facteur existant en spécifiant l'ordre des niveaux."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
Laurence Viry's avatar
Laurence Viry committed
1696
1697
1698
   "metadata": {
    "collapsed": true
   },
Laurence Viry's avatar
Laurence Viry committed
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
   "outputs": [],
   "source": [
    "table(Xqual)\n",
    "Xqual3 <- factor(Xqual,levels=c(\"placebo\", \"nouveau\",\"classique\"))\n",
    "Xqual3 <- Xqual3[-4] # elimine l'individu avec la modalite \"3\"\n",
    "#Xqual3\n",
    "table(Xqual3) # la modalite \"3\" n'apparait plus\n",
    "# elimine la modalite \"3\"\n",
    "Xqual3 <- factor(as.character(Xqual3)) # elimine la modalite \"3\"\n",
    "table(Xqual3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
Laurence Viry's avatar
Laurence Viry committed
1715
1716
    "## Manipuler des individus\n",
    "\n",
Laurence Viry's avatar
Laurence Viry committed
1717
1718
1719
    "### Repérer les individus manquants \n",
    "Dans R, les données manquantes sont représentées par **NA**. La fonction **is.na**\n",
    "permet de les retrouver.\n",
Laurence Viry's avatar
Laurence Viry committed
1720
    "\n",
Laurence Viry's avatar
Laurence Viry committed
1721
1722
1723
1724
1725
1726
    "* Dans une variable:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
Laurence Viry's avatar
Laurence Viry committed
1727
1728
1729
   "metadata": {
    "collapsed": true
   },
Laurence Viry's avatar
Laurence Viry committed
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
   "outputs": [],
   "source": [
    "X <- rnorm(10,0,1)\n",
    "X[c(2,7,10)] <- NA\n",
    "summary(X)\n",
    "mean(X)\n",
    "mean(X,na.rm=TRUE)\n",
    "#\n",
    "selectNA <- is.na(X)\n",
    "selectNA\n",
    "which(selectNA) # Quels sont les indices correspondants\n",
    "# \"!\" : negation \n",
    "X2 <-X[!selectNA] # On élimine les individus correspondants"
Laurence Viry's avatar
Laurence Viry committed
1743
1744
1745
1746
1747
1748
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
Laurence Viry's avatar
Laurence Viry committed
1749
1750
1751
1752
1753
1754
    "* Dans sun tableau de données:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
Laurence Viry's avatar
Laurence Viry committed
1755
1756
1757
   "metadata": {
    "collapsed": true
   },
Laurence Viry's avatar
Laurence Viry committed
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
   "outputs": [],
   "source": [
    "Y <- factor(c(rep(\"A\",3),NA,rep(\"M\",4),\"D\",NA))\n",
    "# data-frame avec les deux variables X et Y\n",
    "don <-data.frame(X,Y)\n",
    "summary(don)\n",
    "# \n",
    "selectNA <- is.na(don)\n",
    "#mode(selectNA)\n",
    "class(selectNA)\n",
    "#selectNA\n",
    "#\n",
    "# au moins une donnee manquante pour un individu\n",
    "aelim_any <- apply(selectNA, MARGIN=1,FUN=any)\n",
    "aelim_any \n",
    "# Toutes les donnees sont manquantes pour un individu\n",
    "aelim_all <- apply(selectNA, MARGIN=1,FUN=all)\n",
    "aelim_all"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
Laurence Viry's avatar
Laurence Viry committed
1781
1782
1783
   "metadata": {
    "collapsed": true
   },
Laurence Viry's avatar
Laurence Viry committed
1784
1785
1786
1787
1788
   "outputs": [],
   "source": [
    "don2 <-don[!aelim_all,] # individus élimines\n",
    "which(is.na(don))\n",
    "which(is.na(don),arr.ind=T) # option arr.ind (array indices) de which"
Laurence Viry's avatar
Laurence Viry committed
1789
1790
1791
1792
1793
1794
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
Laurence Viry's avatar
Laurence Viry committed
1795
    "### Repérer les individus aberrants"
1796
1797
1798
   ]
  },
  {
Laurence Viry's avatar
Laurence Viry committed
1799
   "cell_type": "markdown",
Laurence Viry's avatar
Laurence Viry committed
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
   "metadata": {},
   "source": [
    "On utilise la fonction boxplot qui fournit dans sa composante out les valeurs\n",
    "aberrantes.\n",
    "\n",
    "Dans l'exemple, on utilise le package \"rpart\", vous pouvez l'installer en local, voir ~/Utils/install_local_package.ipynb"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
1811
1812
1813
   "metadata": {
    "collapsed": true
   },
Laurence Viry's avatar
Laurence Viry committed
1814
1815
1816
1817
1818
1819
1820
1821
   "outputs": [],
   "source": [
    "rm(list=ls())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
Laurence Viry's avatar
Laurence Viry committed
1822
1823
1824
   "metadata": {
    "collapsed": true
   },
Laurence Viry's avatar
Laurence Viry committed
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
   "outputs": [],
   "source": [
    "library(rpart)\n",
    "data(\"kyphosis\")\n",
    "names(kyphosis)\n",
    "boxNumber <- boxplot(kyphosis[,\"Number\"]) # repere"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
Laurence Viry's avatar
Laurence Viry committed
1836
1837
1838
   "metadata": {
    "collapsed": true
   },
Laurence Viry's avatar
Laurence Viry committed
1839
   "outputs": [],
Laurence Viry's avatar
Laurence Viry committed
1840
   "source": [
Laurence Viry's avatar
Laurence Viry committed
1841
1842
1843
1844
1845
1846
1847
    "class(boxNumber)\n",
    "attributes(boxNumber)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
Laurence Viry's avatar
Laurence Viry committed
1848
1849
1850
   "metadata": {
    "collapsed": true
   },
Laurence Viry's avatar
Laurence Viry committed
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
   "outputs": [],
   "source": [
    "# les individus aberrants\n",
    "valaberrante <- boxNumber$out\n",
    "#kyphosis[,\"Number\"]%in%valaberrante\n",
    "which(kyphosis[,\"Number\"]%in%valaberrante)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Concaténer des tableaux de données\n",
    "\n",
    "Pour regrouper deux tableaux peut être vu de deux façons:\n",
    "\n",
    "* Aggréger des individus sur lesquels ont été observées les mêmes variables en concaténant des tableaux de données l'un en dessous de l'autre avec la fonction **rbind**."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
Laurence Viry's avatar
Laurence Viry committed
1873
1874
1875
   "metadata": {
    "collapsed": true
   },
Laurence Viry's avatar
Laurence Viry committed
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
   "outputs": [],
   "source": [
    "# exemple rbind sur des matrices avec le même nombre de collonnes\n",
    "X <- matrix(21:24,ncol=2)\n",
    "colnames(X) <- paste(\"X\",1:2,sep=\"\")\n",
    "rownames(X) <- paste(\"individu\",1:2,sep=\"\")\n",
    "X\n",
    "Y <- matrix(11:14,ncol=2)\n",
    "colnames(Y) <- paste(\"Y\",1:2,sep=\"\")\n",
    "rownames(Y) <- paste(\"individu\",3:4,sep=\"\")\n",
    "Y\n",
    "Z <-rbind(X,Y)\n",
    "Z\n",
    "class(Z)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Les deux matrices **X**et **Y** n'ont pas les mêmes noms de collonne, la matrice issue de la concaténation prend les noms de la première matrice.\n",
    "\n",
    "* pour la concaténation des data-frames, il sera nécessaire que les deux data-frame aient les mêmes noms de variables. dans la cas contraire, il sera nécessaire de renommer les variables d'un data-frame."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
Laurence Viry's avatar
Laurence Viry committed
1904
1905
1906
   "metadata": {
    "collapsed": true
   },
Laurence Viry's avatar
Laurence Viry committed
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
   "outputs": [],
   "source": [
    "Xd <- data.frame(X)\n",
    "Yd <- data.frame(Y)\n",
    "Zd <- rbind(Xd,Yd)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
Laurence Viry's avatar
Laurence Viry committed
1917
1918
1919
   "metadata": {
    "collapsed": true
   },
Laurence Viry's avatar
Laurence Viry committed
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
   "outputs": [],
   "source": [
    "colnames(Yd) <- colnames(Xd)\n",
    "Zd <- rbind(Xd,Yd)\n",
    "Zd\n",
    "class(Zd)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* Aggréger des variables qui ont été observées sur les même individus en concaténant des tableaux de données l'un à coté de l'autre avec la fonction **cbind()**."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
Laurence Viry's avatar
Laurence Viry committed
1938
1939
1940
   "metadata": {
    "collapsed": true
   },
Laurence Viry's avatar
Laurence Viry committed
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
   "outputs": [],
   "source": [
    "Xd <- data.frame(matrix(21:24,ncol=2))\n",
    "colnames(Xd) <- paste(\"X\",1:2,sep=\"\")\n",
    "rownames(Xd) <- paste(\"individu\",1:2,sep=\"\")\n",
    "Xd\n",
    "Yd <- data.frame(matrix(11:14,ncol=2))\n",
    "colnames(Yd) <- paste(\"Y\",1:2,sep=\"\")\n",
    "rownames(Yd) <- paste(\"individu\",3:4,sep=\"\")\n",
    "Yd\n",
    "Wd <-cbind(Xd,Yd)\n",
    "Wd\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "La fonction **cbind()** ne vérifie pas le nom des lignes, les noms des lignes du premier data-frame sont conservés. \n",
    "\n",
    "* Il est possible de fusionner deux tableaux selon une **clef** avec la fonction **merge**.\n",
    "\n",
    "Concaténons deux tableaux de données:\n",
    "  - le premier tableau regroupe une variable continue (age) et deux variables qualitatives (\"prenom\" , \"ville\")."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
Laurence Viry's avatar
Laurence Viry committed
1970
1971
1972
   "metadata": {
    "collapsed": true
   },
Laurence Viry's avatar
Laurence Viry committed
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
   "outputs": [],
   "source": [
    "age <- c(45,32,67)\n",
    "ville <- factor(c(\"rennes\",\"rennes\",\"marseille\"))\n",
    "prenom <- c(\"Alice\",\"Marcel\",\"Alexis\")\n",
    "indiv <- cbind.data.frame(age, prenom, ville)\n",
    "class(indiv)\n",
    "indiv"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "  - le second tableau regroupe les caractéristiques des villes."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
Laurence Viry's avatar
Laurence Viry committed
1993
1994
1995
   "metadata": {
    "collapsed": true
   },
Laurence Viry's avatar
Laurence Viry committed
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
   "outputs": [],
   "source": [
    "population <- c(300,500,600)\n",
    "caractVille <- cbind.data.frame(ville =c(\"rennes\",\"lyon\",\"marseille\"),pop=population)\n",
    "caractVille"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "On fusionne les tableaux en un seul où seront répétées les caractéristiques des villes à chaque ligne du tableau. On effectue une fusion avec la fonction **merge()** et la clef **ville**."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
Laurence Viry's avatar
Laurence Viry committed
2013
2014
2015
   "metadata": {
    "collapsed": true
   },
Laurence Viry's avatar
Laurence Viry committed
2016
2017
2018
2019
2020
   "outputs": [],
   "source": [
    "merge(indiv,caractVille,by=\"ville\")"
   ]
  },
Laurence Viry's avatar
Laurence Viry committed
2021
2022
2023
2024
2025
2026
2027
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*exercices*: ~/CED-IntroR/TP/enonces/ImportFusion.pdf"
   ]
  },
Laurence Viry's avatar
Laurence Viry committed
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Tableaux croisés\n",
    "\n",
    "Lorsqu'on a deux variables qualitatives observées sur un échantillon, les données\n",
    "peuvent être présentées sous deux formes:\n",
    "\n",
    "### Tableau de contingences"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
Laurence Viry's avatar
Laurence Viry committed
2043
2044
2045
   "metadata": {
    "collapsed": true
   },
Laurence Viry's avatar
Laurence Viry committed
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
   "outputs": [],
   "source": [
    "tension <- factor(c(rep(\"Faible\",5),rep(\"Forte\",5)))\n",
    "tension\n",
    "laine <- factor(c(rep(\"Mer\",3),rep(\"Ang\",3),rep(\"Tex\",4)))\n",
    "laine\n",
    "# fusionnons ces deux variables dans un data.frame\n",
    "don <-data.frame(tension, laine) # cbind.data.frame(tension,laine)\n",
    "# Tableau de contingences\n",
    "tabcroise <-table(don$tension,don$laine)\n",
    "class(tabcroise)\n",
    "tabcroise"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Tableaux Individus X Variables"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
Laurence Viry's avatar
Laurence Viry committed
2070
2071
2072
   "metadata": {
    "collapsed": true
   },
Laurence Viry's avatar
Laurence Viry committed
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
   "outputs": [],
   "source": [
    "tabframe <- as.data.frame(tabcroise)\n",
    "tabframe"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Nous obtenons une fréquence pour chaque combinaison et non pas une\n",
    "ligne par individu. (pas très compliqué à obtenir, voir exercice indVarTab.ipynb)"
   ]
  },
  {
   "cell_type": "markdown",
Laurence Viry's avatar
Laurence Viry committed
2089
   "metadata": {},
Laurence Viry's avatar
Laurence Viry committed
2090
   "source": [
Laurence Viry's avatar
Laurence Viry committed
2091
    "## Manipuler des données avec dplyr\n",
Laurence Viry's avatar
Laurence Viry committed
2092
    "\n",
Laurence Viry's avatar
Laurence Viry committed
2093
    "**dplyr** est un package facilitant le traitement et la manipulation de données contenues dans une ou plusieurs tables, la manipulation de données se fait en utilisant un nombre réduit de **verbes**, qui correspondent chacun à une action différente appliquée à un tableau de données.\n",
Laurence Viry's avatar
Laurence Viry committed
2094
    "\n",
Laurence Viry's avatar
Laurence Viry committed
2095
    "Les fonctions de dplyr sont en général plus rapides que leur équivalent sous R de base, elles permettent donc de traiter des données de grande dimension.\n",
Laurence Viry's avatar
Laurence Viry committed
2096
    "\n",
Laurence Viry's avatar
Laurence Viry committed
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
    "**dplyr** fait partie du coeur du **tidyverse**, elle est donc chargée automatiquement avec :"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "library(tidyverse)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# lecture des donnees\n",
    "library(nycflights13)\n",
    "data(flights)\n",
    "#data(airports)\n",
    "#data(airlines)\n",
    "#\n",
    "arrange(select(filter(flights, dest == \"LAX\"), dep_delay, arr_delay), dep_delay)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Les verbes de dplyr\n",
2129
    "\n",
Laurence Viry's avatar
Laurence Viry committed
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
    "* **SLICE** : sélectionne des lignes du tableau selon leur position."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Selectionner la 345eme ligne du tableau airports\n",
    "slice(airports, 345)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* **filter** : sélectionne des lignes d’un tableau de données selon une condition."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Selectionner les vols du mois de janvier en filtrant sur la variable month\n",
    "filter(flights, month == 1)\n",
    "# Vols avec un retard au départ (variable dep_delay) compris entre 10 et 15 minutes \n",
    "filter(flights, dep_delay >= 10 & dep_delay <= 15)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* **select** : permet de sélectionner des colonnes d’un tableau de données. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Extraire les colonnes lat et lon du tableau airports\n",
    "select(airports, lat, lon)\n",
    "# Eliminer les colonnes lat et lon du tableau airport\n",
    "select(airports, -lat, -lon)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* **rename** : permet de renommer facilement des colonnes d'un tableau de données (nouveau_nom = ancien_nom)."
Laurence Viry's avatar
Laurence Viry committed
2186
   ]
2187
2188
2189
2190
  },
  {
   "cell_type": "code",
   "execution_count": null,
Laurence Viry's avatar
Laurence Viry committed
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
   "metadata": {},
   "outputs": [],
   "source": [
    "# Renommer les colonnes lon et lat de airports en longitude et latitude \n",
    "rename(airports, longitude = lon, latitude = lat)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* ** arrange** : réordonne les lignes d’un tableau selon une ou plusieurs colonnes."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Trier le tableau flights selon le retard au départ croissant \n",
    "arrange(flights, dep_delay)\n",
    "# Trier le tableau flights selon selon le mois, puis selon le retard au départ\n",
    "arrange(flights, month, dep_delay)\n",
    "# Trier selon une colonne par ordre décroissant\n",
    "arrange(flights, desc(dep_delay))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* **mutate** : permet de créer de nouvelles colonnes dans le tableau de données, en général à partir de variables existantes."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# airports contient l’altitude en pieds, créer une nouvelle variable alt_m avec l’altitude en mètres\n",
    "airports <- mutate(airports, alt_m = alt / 3.2808)\n",
    "select(airports, name, alt, alt_m)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**mutate** est compatible avec les fonctions de recodages : forcats, if_else, case_when …"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "flights <- mutate(flights,\n",
    "                  type_retard = case_when(\n",
    "                    dep_delay > 0 & arr_delay > 0 ~ \"Retard départ et arrivée\",\n",
    "                    dep_delay > 0 & arr_delay <= 0 ~ \"Retard départ\",\n",
    "                    dep_delay <= 0 & arr_delay > 0 ~ \"Retard arrivée\",\n",
    "                    TRUE ~ \"Aucun retard\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Enchaîner les opérations avec le pipe\n",
    "\n",
    "Quand on manipule un tableau de données, il est très fréquent d’enchaîner plusieurs opérations. On va par exemple filtrer pour extraire une sous-population, sélectionner des colonnes puis trier selon une variable.\n",
    "\n",
    "#### Plusieurs méthodes\n",
    "\n",
    "* Effectuer toutes les opérations en une fois en les “emboîtant” "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* Effectuer les opérations les unes après les autres, en stockant les résultats intermédiaires dans un objet temporaire."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tmp <- filter(flights, dest == \"LAX\")\n",
    "tmp <- select(tmp, dep_delay, arr_delay)\n",
    "arrange(tmp, dep_delay)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* Utiliser l'opérateur **pipe** noté **%>%**\n",
    "\n",
    "**expr %>% f**, le résultat de l’expression *expr*, à gauche du pipe, sera passé comme premier argument à la fonction *f*, à droite du pipe, ce qui revient à exécuter **f(expr)**."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# filter(flights, dest == \"LAX\")\n",
    "flights %>% filter(dest == \"LAX\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# select(filter(flights, dest == \"LAX\"), dep_delay, arr_delay)\n",
    "flights %>% filter(dest == \"LAX\") %>% select(dep_delay, arr_delay)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Pour en savoir plus consulter : https://juba.github.io/tidyverse/10-dplyr.html#preparation-2"
   ]
  },
  {
   "cell_type": "markdown",
2327
2328
2329
   "metadata": {
    "collapsed": true
   },
Laurence Viry's avatar
Laurence Viry committed
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
   "source": [
    "### Pour en savoir plus:\n",
    "\n",
    "* [Introduction à R et au tidyverse](https://juba.github.io/tidyverse/index.html)\n",
    "\n",
    "* [Gestion des données avec R](https://www.fun-mooc.fr/c4x/UPSUD/42001S03/asset/data-management.html) (Christophe Lalanne & Bruno Falissard -MOOC \"Introduction à la statistique avec R\").\n",
    "\n",
    "* [Begin'R (Bordeaux INP](http://beginr.u-bordeaux.fr/index.html#sommaire))\n",
    "\n",
    "* [Cookbook for R](http://www.cookbook-r.com/Manipulating_data/)\n"
   ]
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
  }
 ],
 "metadata": {
  "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",
Laurence Viry's avatar
Laurence Viry committed
2355
   "version": "3.4.3"
2356
2357
2358
2359
2360
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}