generated from intro-to-data-science-22-workshop/00-template-munzert-kermit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
02-cleaning-dirty-data-with-janitor-presentation.Rmd
588 lines (427 loc) · 12.4 KB
/
02-cleaning-dirty-data-with-janitor-presentation.Rmd
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
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
---
title: "Cleaning dirty data with {janitor}"
subtitle: "Workshop: I2DS Tools for Data Science "
author: "Abigail Pena Alejos & Nikolina Klatt"
institute: "Hertie School"
date: "November 21st 2022"
output:
xaringan::moon_reader:
css: [default, metropolis, metropolis-fonts]
lib_dir: presentation_files
nature:
highlightStyle: github
highlightLines: true
countIncrementalSlides: false
ratio: '16:9'
hash: true
---
```{css, echo=FALSE}
@media print { # print out incremental slides; see https://stackoverflow.com/questions/56373198/get-xaringan-incremental-animations-to-print-to-pdf/56374619#56374619
.has-continuation {
display: block !important;
}
}
```
```{r setup, include=FALSE}
# figures formatting setup
options(htmltools.dir.version = FALSE)
library(tidyverse)
library(janitor)
library(kableExtra)
library(rvest)
library(httr)
library(knitr)
library(readxl)
opts_chunk$set(
prompt = T,
fig.align="center", #fig.width=6, fig.height=4.5,
# out.width="748px", #out.length="520.75px",
dpi=300, #fig.path='Figs/',
cache=F, #echo=F, warning=F, message=F
engine.opts = list(bash = "-l")
)
```
# Agenda
<br>
.pull-left[
<br>
1. Overview
2. Data cleaning
3. Data exploring
4. Resources
5. Summary
]
.pull-right-center[
<div align="left">
<br>
<img src="images/nyt_data.png" width=400>
</div>
[The New York Times](https://www-1nytimes-1com-15dsaj73a00d2.hertie.hh-han.com/2014/08/18/technology/for-big-data-scientists-hurdle-to-insights-is-janitor-work.html?searchResultPosition=1)
]
.pull-right-center[
<div align="left">
<br>
<img src="images/janitor.jpeg" width=450>
</div>
]
---
# Overview
.pull-left[
<br><br>
{**janitor**} is a package built by [**Sam Firke**](https://samfirke.com/about/).
- Simple functions for **cleaning** and **examining** data
- Optimized for **user-friendliness**
- For beginning and intermediate R users
- Advanced R users: with {janitor} they can do data wrangling faster
Let's get started <br>
--> `install.packages("janitor")` <br>
--> `library(janitor)`
]
.pull-right[
<div align="left">
<br>
<img src="images/janitor_firke.png" width=500>
</div>
<div align="left">
<br>
<img src="images/github_janitor.png" width=500>
</div>
[GitHub janitor](https://github.com/sfirke/janitor)
]
---
# Cleaning data
<br>
**Clean data frame names with `clean_names()`**
.pull-left[
##tidyverse grammar
Consistent variable names are important
**snake_case** <br>
*“Variable and function names should use only lowercase letters, numbers, and underscores. Use underscores (so called snake_case) to separate words within a name.”* <br>
[The tidyverse style guide](https://style.tidyverse.org/syntax.html)
]
.pull-right[
<div align="left">
<br>
<img src="images/tidydata.jpeg" width=500>
</div>
[Julie Lowndes and Allison Horst](https://www.openscapes.org/blog/2020/10/12/tidy-data/)
]
---
# Cleaning data
<br>
**Clean data frame names with `clean_names()`**
.pull-left[
```{r}
# Create a test data frame with dirty variable names
dirty_df <- as.data.frame(matrix(ncol = 7))
names(dirty_df) <- c("firstName", "a@b!c'ß??",
"success.rate.in.%.(2022)",
"IDENTICAL", "IDENTICAL",
"", "#")
clean_df <- dirty_df %>%
clean_names()
colnames(clean_df)
```
]
---
# Cleaning data
<br>
**Clean data frame names with `clean_names()`**
.pull-left[
```{r}
# Create a test data frame with dirty variable names
dirty_df <- as.data.frame(matrix(ncol = 7))
names(dirty_df) <- c("firstName", "a@b!c'ß??",
"success.rate.in.%.(2022)",
"IDENTICAL", "IDENTICAL",
"", "#")
clean_df <- dirty_df %>%
clean_names()
colnames(clean_df)
```
]
.pull-right[
+ **Consistent format** for letter cases and separators
+ **snake_case** is default, but other cases like camelCase are available
+ Handles **special characters** and **spaces**, including transliterating characters like `ß` to `ss`
+ Converts "%" to "**percent**" and "#" to "**number**"
+ Adds numbers to **duplicated names**
+ **Spacing** (or lack thereof) around numbers is preserved
- Works with the **%>% operator**
- Can be used for data frames and objects
- `make_names_clean` also works for character vectors
]
---
# Cleaning data
```{r}
# install.packages("palmerpenguins")
library(palmerpenguins)
dirty_penguins_df <- penguins_raw
colnames(dirty_penguins_df)
```
---
# Cleaning data
```{r}
# install.packages("palmerpenguins")
library(palmerpenguins)
dirty_penguins_df <- penguins_raw
colnames(dirty_penguins_df)
clean_penguins_df <- penguins_raw %>%
clean_names()
colnames(clean_penguins_df)
```
---
# Cleaning data
<br>
**Remove content with `remove_empty()`**
- `remove_empty()` rows and columns <br>
+ Cleans Excel files that contain empty rows and columns after being read into R <br>
+ Adding `quiet = FALSE` let's you know how many rows or columns were removed.
```{r}
empty_df <- data.frame(v1 = c(1, NA, 3),
v2 = c(NA, NA, NA),
v3 = c("a", NA, "b"))
empty_df %>%
remove_empty(c("rows", "cols"), quiet = FALSE) %>%
glimpse
```
---
# Cleaning data
<br>
**Drop constant columns with `remove_constant()`**
.pull-left[
`remove_constant()` columns <br>
Drops columns from a data frame that contain only a single constant value
```{r}
adelie_df <- clean_penguins_df %>%
filter(species == "Adelie Penguin (Pygoscelis adeliae)")
adelie_df %>%
names()
```
]
.pull-right[
```{r}
adelie_clean_df <- adelie_df %>%
remove_constant()
adelie_clean_df %>%
names()
```
]
---
# Cleaning data
<br>
**Check content of columns with `compare_df_cols()`**
.pull-left[
Imagine you have a set of data frames that you want to combine by binding the rows together but `rbind()` fails. You can check if the column classes match and see if they are **matching** by running `compare_df_cols()`.
- takes unquoted names of data frames, tibbles, or a list of data frames <br>
--> Returns a summary of how they compare
- What column types are there?
- How do column types differ?
- Which are missing or present in the different inputs?
]
.pull-right[
```{r}
chinstrap_df <- clean_penguins_df %>%
filter(species == "Chinstrap penguin (Pygoscelis antarctica)")
# rbind(adelie, chinstrap)
compare_df_cols(adelie_clean_df, chinstrap_df) %>%
tail()
```
]
---
# Exploring data
<br>
**`tabyl()` – a tidy, fully-featured approach to counting things**
.pull-left[
Why not use `table()`?
- It doesn't work with the `%>%` operator
- It doesn't output data frames
- Its results are hard to format
]
.pull-right[
**One variable**
```{r}
table(clean_penguins_df$sex)
```
]
---
# Exploring data
<br>
**`tabyl()` – a tidy, fully-featured approach to counting things**
.pull-left[
Why not use `table()`?
- It doesn't work with the `%>%` operator
- It doesn't output data frames
- Its results are hard to format
Instead better use `tabyl()`
- Tidyverse-aligned - primarily built upon the `{dplyr}` and `{tidyr}` packages
- Compatible with the `{knitr}` package
- Useful for data exploration
- Generate frequencies along with the percent of total
- Counts combinations of one, two, or three variables
]
.pull-right[
**One variable**
```{r}
table(clean_penguins_df$sex)
tabyl(clean_penguins_df$sex)
clean_penguins_no_na_df <- clean_penguins_df %>%
drop_na(sex)
```
]
---
# Exploring data
<br>
**`tabyl()` – a tidy, fully-featured approach to counting things**
**Two variables**
Two-way tabyl / "crosstab" or "contingency" table
```{r}
clean_penguins_no_na_df %>%
tabyl(island, sex)
```
---
# Exploring data
<br>
**`tabyl()` – a tidy, fully-featured approach to counting things**
**Two variables**
Two-way tabyl / "crosstab" or "contingency" table
```{r}
penguins_crosstab_table <- clean_penguins_no_na_df %>%
tabyl(island, sex) %>%
adorn_totals("col") %>% # total in each row
adorn_percentages("row") %>% # percentage value per row
adorn_pct_formatting(digits = 2) %>% # rounded percentage value
adorn_ns() %>% # adds the absolute numbers
adorn_title() # adds the variable name
penguins_crosstab_table
```
---
# Exploring data
<br>
**`adorn_*()` options**
.pull-left[
- `tabyl()` can be formatted with a suite of `adorn_*` functions to add information and for pretty formatting: <br>
+ **`adorn_totals()`**: Add totals row, column, or both.
+ **`adorn_percentages()`**: Calculate percentages along either axis or the entire tabyl
+ **`adorn_pct_formatting()`**: Format percentage columns, controlling the number of digits to display and whether to append the `%` symbol
+ **`adorn_rounding()`**: Round a data frame of numbers
]
.pull-right[
<br><br><br>
+ **`adorn_ns()`**: Add Ns to a tabyl - drawn from the tabyl's underlying counts or they can be supplied by the user
+ **`adorn_title()`**: Add a title to a tabyl - pptions include putting the column title in a new row on top of the data frame or combining the row and column titles in the data frame's first name slot.
]
---
# Exploring data
**`tabyl()`** **Three variables**
Three-way tabyl
```{r}
clean_penguins_no_na_df %>% tabyl(island, species, sex) %>%
adorn_percentages("all") %>%
adorn_pct_formatting(digits = 1) %>%
adorn_title() %>%
kable()
```
---
# Cleaning data
<br>
### Fixing dates
.pull-left[
- `excel_numeric_to_date()` <br>
Fix dates stored as serial numbers <br>
Converts serial date numbers from Excel to class `Date`
```{r}
excel_numeric_to_date(44886)
```
]
.pull-right[
- `convert_to_date()` <br>
Convert a mix of date and datetime formats to date
```{r}
convert_to_date(c("2020-02-29", "40000.1"))
convert_to_datetime(40000.1)
```
]
---
# Cleaning data
<br>
### Rounding numbers
.pull-left[
Careful: In base R `round()` uses **"banker's rounding"**, <br>
i.e., halves are rounded to the nearest *even* number.
```{r}
numbers <- c(3.5, 2.5)
round(numbers)
```
Instead use: `round_half_up()` <br>
directionally-consistent rounding behavior <br>
```{r}
round_half_up(numbers)
```
]
.pull-right[
`round_to_fraction()` <br>
round decimals to precise fractions of a given denominator
```{r}
round_to_fraction(0.175, denominator = 4)
round_to_fraction(0.2, denominator = 4)
round_to_fraction(0.25000000001, denominator = 4)
```
]
---
# Exploring data
<br>
**Detect duplicated records with `get_dupes()`**
.pull-left[
Checking for duplicated records is important because they can interfere with your tabulations in the analysis.
{`janitor`} makes this tedious task simple.
`get_dupes()` <br>
- Detects duplicate records during data cleaning
- Returns the records (and inserts a count of duplicates) so you can examine the potentially problematic cases
]
.pull-right[
```{r}
clean_penguins_df %>%
get_dupes(individual_id) %>%
head(6)
```
]
---
# Resources
<br>
.pull-left[
Original material:
+ [Package janitor documentation](https://cran.r-project.org/web/packages/janitor/janitor.pdf)
+ [GitHub Repository](https://github.com/sfirke/janitor)
Further resources:
+ [exploringdata.org - How to Clean Data: {janitor} Package](https://www.exploringdata.org/post/how-to-clean-data-janitor-package/)
+ [towardsdatascience.com - Cleaning and Exploring Data with the “janitor” Package ](https://towardsdatascience.com/cleaning-and-exploring-data-with-the-janitor-package-ee4a3edf085e)
+ [jenrichmond.rbind.io - Cleaning penguins with the janitor package ](http://jenrichmond.rbind.io/post/digging-into-the-janitor-package/)
]
.pull-right[
More on tidying data:<br>
[tidyr cheatsheet](https://github.com/rstudio/cheatsheets/blob/main/tidyr.pdf)
Unfortunately, there is no cheatsheet for {`janitor`}. <br>
Here are two alternative tips:
- `ls("package:janitor")` gives you a list of all functions
- or type `janitor::` in your console and you get to scroll up and down the list of all functions in the package.
BTW, this works for all packages ;)
]
---
# Summary
<br>
.pull-left[
+ Integrate `{janitor}` into your data wrangling and cleaning pipeline
+ It works faster than regular functions from the `{tidyverse}`
+ It can help you to:
+ **quickly** clean variable names
+ remove empty rows and columns
+ remove constant columns
+ **easily** create **better** tables that work in the tidyverse, can be stored as data frames and are almost worth publishing!
]
.pull-right[
<div align="left">
<img src="images/unsplash.jpg" width=500>
</div>
## **Happy data cleaning!**
]