-
Notifications
You must be signed in to change notification settings - Fork 0
/
presentation.Rpres
347 lines (291 loc) · 6.93 KB
/
presentation.Rpres
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
Intuitive big data manipulation with dplyr and tidyr
========================================================
author: Vlad Achimescu
date: 03/05/2019
autosize: false
css: presstyle.css
Download presentation and data here:
https://github.com/nomoteticus/R_data_manipulation
Summary
========================================================
<hr>
Data manipulation:
- import data into a tibble/data frame
- filter data -> select cases
- subset columns -> select variables
- add new variables to data frame
- aggregate variables
- reshape data
- from columns to rows
- from rows to columns
- merge data
The tidyverse
========================================================
<hr>
![tidy data](img/00_tidyverse.png)
***
<small>
www.tidyverse.com
</small>
- readr
- magrittr
- dplyr
- tidyr
- ggplot -> next tutorial
<small>
```{r eval = FALSE}
install.packages( c("readr","magrittr",
"dplyr","tidyr") )
```
</small>
Principles of tidy data
========================================================
<hr>
![tidy data](img/01_tidy_data.png)
<small><i>(Wickham & Grolemund 2017)</i></small>
Tidy datasets
========================================================
<small>
Untidy
<table>
<tr>
<th><br>id</th>
<th>var</th>
<th>val_jan</th>
<th>val_aug<br></th>
</tr>
<tr>
<td>1</td>
<td>City</td>
<td>Mannheim</td>
<td>Mannheim</td>
</tr>
<tr>
<td>1</td>
<td>temperature</td>
<td>5C / 41F<br></td>
<td>30C / 86F</td>
</tr>
<tr>
<td>2</td>
<td>City</td>
<td>Hamburg</td>
<td>Hamburg</td>
</tr>
<tr>
<td>2</td>
<td>temperature</td>
<td>-5C / 23F</td>
<td>25C / 77F</td>
</tr>
</table>
- Same observation in diff. lines
- No unique measurement in cols
- Strings and numeric in same col
- 2 values in one cell
</table>
</small>
***
-
Tidy datasets
========================================================
<small>
Untidy
<table>
<tr>
<th><br>id</th>
<th>var</th>
<th>val_jan</th>
<th>val_aug<br></th>
</tr>
<tr>
<td>1</td>
<td>City</td>
<td>Mannheim</td>
<td>Mannheim</td>
</tr>
<tr>
<td>1</td>
<td>temperature</td>
<td>5C / 41F<br></td>
<td>30C / 86F</td>
</tr>
<tr>
<td>2</td>
<td>City</td>
<td>Hamburg</td>
<td>Hamburg</td>
</tr>
<tr>
<td>2</td>
<td>temperature</td>
<td>-5C / 23F</td>
<td>25C / 77F</td>
</tr>
</table>
- Same observation in diff. lines
- No unique measurement in cols
- Strings and numeric in same col
- 2 values in one cell
***
Tidy
<table>
<tr>
<th><br>id</th>
<th>City</th>
<th>month</th>
<th><br>temp_C</th>
<th>temp_F</th>
</tr>
<tr>
<td>1</td>
<td>Mannheim</td>
<td>jan</td>
<td>5</td>
<td>41</td>
</tr>
<tr>
<td>2</td>
<td>Mannheim</td>
<td>aug</td>
<td>25</td>
<td>77</td>
</tr>
<tr>
<td>3</td>
<td>Hamburg</td>
<td>jan</td>
<td>-5</td>
<td>23</td>
</tr>
<tr>
<td>4</td>
<td>Hamburg</td>
<td>aug</td>
<td>25</td>
<td>77</td>
</tr>
</table>
- Every line is one case
- Every column is one variable
- Can summarise columns
- Cells are values
</small>
Load data
========================================================
class: at75
<hr>
- <i>readr</i> reads file as tibbles
- tibbles = enhanced data frames
- more classes for columns
- look nicer when printed
```{r load, eval = FALSE}
library(readr)
read_csv(filename)
read_tsv(filename)
read_delim(filename,
sep = "char")
```
---
https://github.com/nomoteticus/R_data_manipulation
Two datasets:
- https://ec.europa.eu/eurostat/data/database
- <b>EMP</b>: Employment rate of the age group 15-64 by NUTS 2 regions [tgs00007] <br> (% employed in each region) <br> <i>file: empl_rates_reg.tsv</i>
- <b>TRAIN</b>: Adult participation in learning by sex [sdg_04_60]<br> (% of population aged 25 to 64) <br> <i>file: traing_participation.csv</i>
- Which one is tidy?
Pipe operator
========================================================
<hr>
![magrittr](img/02_magrittr.png)
$$(f\circ g)(x) = f(g(x))$$
```{r, eval=FALSE}
### Without piping
f(g(arg_g), arg_f)
### With piping
arg_g %>% g %>% f(arg_f)
```
- similar to Python (. operator, e.g. ln(3).round(3))
SUBSETTING ROWS / FILTER
========================================================
<small>
<hr>
![tidy data](img/03_filter.png)
***
.
- select cases that satisfy certain conditions with <i>filter()</i>
- conditions can be separated by commas
```{r, eval=FALSE}
DF = DF %>% filter(condition1, condition2, condition3)
# all must be true to be kept in dataset
```
</small>
SUBSETTING COLUMNS / SELECT
========================================================
<hr>
<large>
![tidy data](img/04_select.png)
</large>
***
.
- select columns by names
- no need to quote variable names
- ranges can also be specified
- similar to Stata
ADDING NEW VARIABLES
========================================================
<hr>
![tidy data](img/05_mutate.png)
- can reference newly created variables
- comma separated
- <i>transmute()</i> keeps only new variables
***
![tidy data](img/05b_mutate.png)
</large>
AGGREGATING DATA
========================================================
<hr>
<hr>
![tidy data](img/06_groupby.png)
![tidy data](img/06b_groupby.png)
***
.
- similar to SQL
- <i>group_by(var1, var2)</i> to define grouping variables
- to aggregate by group, use <i>summarise()</i>
- to add group-level aggregates but keep current unit of analysis, use<br> <i>mutate()</i>
- in the end <i>ungroup()</i> to remove grouping
SUMMARY FUNCTIONS
========================================================
<hr>
<large>
![tidy data](img/07_summarise.png)
</large>
RESHAPING DATASETS
========================================================
<hr>
<large>
![tidy data](img/08_reshape.png)
</large>
- <b>gather()</b>: from wide to long data format
- similar to <i>melt</i> from <i>reshape</i> package
- <b>spread()</b>: from long to wide data format
- similar to <i>cast</i> from <i>reshape</i> package
MERGING DIFFERENT DATASETS
========================================================
class: at80
<hr>
<large>
![tidy data](img/09_merge.png)
</large>
- x1 is key value
- similar to <i>merge</i> from base R and SQL JOIN clauses
SOME RESOURCES
========================================================
<hr>
- Wickham, Hadley; Grolemund, Garrett (2017): <i>R for data science. Import, tidy, transform, visualize, and model data.</i> Beijing: O'Reilly.
- www.tidyverse.com
- <a href = "https://cran.r-project.org/web/packages/dplyr/vignettes/dplyr.html"> Introduction to <i>dplyr</i> @ CRAN </a>
- <a href = "https://cran.r-project.org/web/packages/tidyr/vignettes/tidy-data.html"> Introduction to <i>tidyr</i> @ CRAN <a>
- <a href = "https://www.datacamp.com/courses?q=topic%3Adata_manipulation"> <i>datacamp</i> courses on data manipulation </a>
- <a href = "https://www.rstudio.com/wp-content/uploads/2015/02/data-wrangling-cheatsheet.pdf"> cheat sheet @ RStudio.com</a>
![tidy data](img/10_cheatsheet.png)