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.html
1005 lines (799 loc) · 26.1 KB
/
02-cleaning-dirty-data-with-janitor-presentation.html
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
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
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
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
<!DOCTYPE html>
<html lang="" xml:lang="">
<head>
<title>Cleaning dirty data with {janitor}</title>
<meta charset="utf-8" />
<meta name="author" content="Abigail Pena Alejos & Nikolina Klatt" />
<script src="presentation_files/header-attrs/header-attrs.js"></script>
<link href="presentation_files/remark-css/default.css" rel="stylesheet" />
<link href="presentation_files/remark-css/metropolis.css" rel="stylesheet" />
<link href="presentation_files/remark-css/metropolis-fonts.css" rel="stylesheet" />
</head>
<body>
<textarea id="source">
class: center, middle, inverse, title-slide
.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
]
---
<style type="text/css">
@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;
}
}
</style>
# 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)
```
```
## [1] "first_name" "a_b_css"
## [3] "success_rate_in_percent_2022" "identical"
## [5] "identical_2" "x"
## [7] "number"
```
]
---
# 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)
```
```
## [1] "first_name" "a_b_css"
## [3] "success_rate_in_percent_2022" "identical"
## [5] "identical_2" "x"
## [7] "number"
```
]
.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)
```
```
## [1] "studyName" "Sample Number" "Species"
## [4] "Region" "Island" "Stage"
## [7] "Individual ID" "Clutch Completion" "Date Egg"
## [10] "Culmen Length (mm)" "Culmen Depth (mm)" "Flipper Length (mm)"
## [13] "Body Mass (g)" "Sex" "Delta 15 N (o/oo)"
## [16] "Delta 13 C (o/oo)" "Comments"
```
---
# Cleaning data
```r
> # install.packages("palmerpenguins")
> library(palmerpenguins)
> dirty_penguins_df <- penguins_raw
> colnames(dirty_penguins_df)
```
```
## [1] "studyName" "Sample Number" "Species"
## [4] "Region" "Island" "Stage"
## [7] "Individual ID" "Clutch Completion" "Date Egg"
## [10] "Culmen Length (mm)" "Culmen Depth (mm)" "Flipper Length (mm)"
## [13] "Body Mass (g)" "Sex" "Delta 15 N (o/oo)"
## [16] "Delta 13 C (o/oo)" "Comments"
```
```r
> clean_penguins_df <- penguins_raw %>%
+ clean_names()
> colnames(clean_penguins_df)
```
```
## [1] "study_name" "sample_number" "species"
## [4] "region" "island" "stage"
## [7] "individual_id" "clutch_completion" "date_egg"
## [10] "culmen_length_mm" "culmen_depth_mm" "flipper_length_mm"
## [13] "body_mass_g" "sex" "delta_15_n_o_oo"
## [16] "delta_13_c_o_oo" "comments"
```
---
# 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
```
```
## Removing 1 empty rows of 3 rows total (33.3%).
```
```
## Removing 1 empty columns of 3 columns total (Removed: v2).
```
```
## Rows: 2
## Columns: 2
## $ v1 <dbl> 1, 3
## $ v3 <chr> "a", "b"
```
---
# 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()
```
```
## [1] "study_name" "sample_number" "species"
## [4] "region" "island" "stage"
## [7] "individual_id" "clutch_completion" "date_egg"
## [10] "culmen_length_mm" "culmen_depth_mm" "flipper_length_mm"
## [13] "body_mass_g" "sex" "delta_15_n_o_oo"
## [16] "delta_13_c_o_oo" "comments"
```
]
.pull-right[
```r
> adelie_clean_df <- adelie_df %>%
+ remove_constant()
>
> adelie_clean_df %>%
+ names()
```
```
## [1] "study_name" "sample_number" "island"
## [4] "individual_id" "clutch_completion" "date_egg"
## [7] "culmen_length_mm" "culmen_depth_mm" "flipper_length_mm"
## [10] "body_mass_g" "sex" "delta_15_n_o_oo"
## [13] "delta_13_c_o_oo" "comments"
```
]
---
# 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()
```
```
## column_name adelie_clean_df chinstrap_df
## 12 region <NA> character
## 13 sample_number numeric numeric
## 14 sex character character
## 15 species <NA> character
## 16 stage <NA> character
## 17 study_name character character
```
]
---
# 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)
```
```
##
## FEMALE MALE
## 165 168
```
]
---
# 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)
```
```
##
## FEMALE MALE
## 165 168
```
```r
> tabyl(clean_penguins_df$sex)
```
```
## clean_penguins_df$sex n percent valid_percent
## FEMALE 165 0.47965116 0.4954955
## MALE 168 0.48837209 0.5045045
## <NA> 11 0.03197674 NA
```
```r
> 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)
```
```
## island FEMALE MALE
## Biscoe 80 83
## Dream 61 62
## Torgersen 24 23
```
---
# 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
```
```
## sex
## island FEMALE MALE Total
## Biscoe 49.08% (80) 50.92% (83) 100.00% (163)
## Dream 49.59% (61) 50.41% (62) 100.00% (123)
## Torgersen 51.06% (24) 48.94% (23) 100.00% (47)
```
---
# 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()
```
<table class="kable_wrapper">
<tbody>
<tr>
<td>
<table>
<thead>
<tr>
<th style="text-align:left;"> </th>
<th style="text-align:left;"> species </th>
<th style="text-align:left;"> </th>
<th style="text-align:left;"> </th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left;"> island </td>
<td style="text-align:left;"> Adelie Penguin (Pygoscelis adeliae) </td>
<td style="text-align:left;"> Chinstrap penguin (Pygoscelis antarctica) </td>
<td style="text-align:left;"> Gentoo penguin (Pygoscelis papua) </td>
</tr>
<tr>
<td style="text-align:left;"> Biscoe </td>
<td style="text-align:left;"> 13.3% </td>
<td style="text-align:left;"> 0.0% </td>
<td style="text-align:left;"> 35.2% </td>
</tr>
<tr>
<td style="text-align:left;"> Dream </td>
<td style="text-align:left;"> 16.4% </td>
<td style="text-align:left;"> 20.6% </td>
<td style="text-align:left;"> 0.0% </td>
</tr>
<tr>
<td style="text-align:left;"> Torgersen </td>
<td style="text-align:left;"> 14.5% </td>
<td style="text-align:left;"> 0.0% </td>
<td style="text-align:left;"> 0.0% </td>
</tr>
</tbody>
</table>
</td>
<td>
<table>
<thead>
<tr>
<th style="text-align:left;"> </th>
<th style="text-align:left;"> species </th>
<th style="text-align:left;"> </th>
<th style="text-align:left;"> </th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left;"> island </td>
<td style="text-align:left;"> Adelie Penguin (Pygoscelis adeliae) </td>
<td style="text-align:left;"> Chinstrap penguin (Pygoscelis antarctica) </td>
<td style="text-align:left;"> Gentoo penguin (Pygoscelis papua) </td>
</tr>
<tr>
<td style="text-align:left;"> Biscoe </td>
<td style="text-align:left;"> 13.1% </td>
<td style="text-align:left;"> 0.0% </td>
<td style="text-align:left;"> 36.3% </td>
</tr>
<tr>
<td style="text-align:left;"> Dream </td>
<td style="text-align:left;"> 16.7% </td>
<td style="text-align:left;"> 20.2% </td>
<td style="text-align:left;"> 0.0% </td>
</tr>
<tr>
<td style="text-align:left;"> Torgersen </td>
<td style="text-align:left;"> 13.7% </td>
<td style="text-align:left;"> 0.0% </td>
<td style="text-align:left;"> 0.0% </td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
---
# 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)
```
```
## [1] "2022-11-21"
```
]
.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"))
```
```
## [1] "2020-02-29" "2009-07-06"
```
```r
> convert_to_datetime(40000.1)
```
```
## [1] "2009-07-06 02:24:00 UTC"
```
]
---
# 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)
```
```
## [1] 4 2
```
Instead use: `round_half_up()` <br>
directionally-consistent rounding behavior <br>
```r
> round_half_up(numbers)
```
```
## [1] 4 3
```
]
.pull-right[
`round_to_fraction()` <br>
round decimals to precise fractions of a given denominator
```r
> round_to_fraction(0.175, denominator = 4)
```
```
## [1] 0.25
```
```r
> round_to_fraction(0.2, denominator = 4)
```
```
## [1] 0.25
```
```r
> round_to_fraction(0.25000000001, denominator = 4)
```
```
## [1] 0.25
```
]
---
# 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)
```
```
## # A tibble: 6 × 18
## individual_id dupe_c…¹ study…² sampl…³ species region island stage clutc…⁴
## <chr> <int> <chr> <dbl> <chr> <chr> <chr> <chr> <chr>
## 1 N11A1 2 PAL0708 21 Adelie… Anvers Biscoe Adul… Yes
## 2 N11A1 2 PAL0809 47 Gentoo… Anvers Biscoe Adul… No
## 3 N11A2 2 PAL0708 22 Adelie… Anvers Biscoe Adul… Yes
## 4 N11A2 2 PAL0809 48 Gentoo… Anvers Biscoe Adul… No
## 5 N12A1 2 PAL0708 23 Adelie… Anvers Biscoe Adul… Yes
## 6 N12A1 2 PAL0809 49 Gentoo… Anvers Biscoe Adul… Yes
## # … with 9 more variables: date_egg <date>, culmen_length_mm <dbl>,
## # culmen_depth_mm <dbl>, flipper_length_mm <dbl>, body_mass_g <dbl>,
## # sex <chr>, delta_15_n_o_oo <dbl>, delta_13_c_o_oo <dbl>,
## # comments <chr>, and abbreviated variable names ¹dupe_count,
## # ²study_name, ³sample_number, ⁴clutch_completion
```
]
---
# 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!**
]
</textarea>
<style data-target="print-only">@media screen {.remark-slide-container{display:block;}.remark-slide-scaler{box-shadow:none;}}</style>
<script src="https://remarkjs.com/downloads/remark-latest.min.js"></script>
<script>var slideshow = remark.create({
"highlightStyle": "github",
"highlightLines": true,
"countIncrementalSlides": false,
"ratio": "16:9",
"hash": true
});
if (window.HTMLWidgets) slideshow.on('afterShowSlide', function (slide) {
window.dispatchEvent(new Event('resize'));
});
(function(d) {
var s = d.createElement("style"), r = d.querySelector(".remark-slide-scaler");
if (!r) return;
s.type = "text/css"; s.innerHTML = "@page {size: " + r.style.width + " " + r.style.height +"; }";
d.head.appendChild(s);
})(document);
(function(d) {
var el = d.getElementsByClassName("remark-slides-area");
if (!el) return;
var slide, slides = slideshow.getSlides(), els = el[0].children;
for (var i = 1; i < slides.length; i++) {
slide = slides[i];
if (slide.properties.continued === "true" || slide.properties.count === "false") {
els[i - 1].className += ' has-continuation';
}
}
var s = d.createElement("style");
s.type = "text/css"; s.innerHTML = "@media print { .has-continuation { display: none; } }";
d.head.appendChild(s);
})(document);
// delete the temporary CSS (for displaying all slides initially) when the user
// starts to view slides
(function() {
var deleted = false;
slideshow.on('beforeShowSlide', function(slide) {
if (deleted) return;
var sheets = document.styleSheets, node;
for (var i = 0; i < sheets.length; i++) {
node = sheets[i].ownerNode;
if (node.dataset["target"] !== "print-only") continue;
node.parentNode.removeChild(node);
}
deleted = true;
});
})();
// add `data-at-shortcutkeys` attribute to <body> to resolve conflicts with JAWS
// screen reader (see PR #262)
(function(d) {
let res = {};
d.querySelectorAll('.remark-help-content table tr').forEach(tr => {
const t = tr.querySelector('td:nth-child(2)').innerText;
tr.querySelectorAll('td:first-child .key').forEach(key => {
const k = key.innerText;
if (/^[a-z]$/.test(k)) res[k] = t; // must be a single letter (key)
});
});
d.body.setAttribute('data-at-shortcutkeys', JSON.stringify(res));
})(document);
(function() {
"use strict"
// Replace <script> tags in slides area to make them executable
var scripts = document.querySelectorAll(
'.remark-slides-area .remark-slide-container script'
);
if (!scripts.length) return;
for (var i = 0; i < scripts.length; i++) {
var s = document.createElement('script');
var code = document.createTextNode(scripts[i].textContent);
s.appendChild(code);
var scriptAttrs = scripts[i].attributes;
for (var j = 0; j < scriptAttrs.length; j++) {
s.setAttribute(scriptAttrs[j].name, scriptAttrs[j].value);
}
scripts[i].parentElement.replaceChild(s, scripts[i]);
}
})();
(function() {
var links = document.getElementsByTagName('a');
for (var i = 0; i < links.length; i++) {
if (/^(https?:)?\/\//.test(links[i].getAttribute('href'))) {
links[i].target = '_blank';
}
}
})();
// adds .remark-code-has-line-highlighted class to <pre> parent elements
// of code chunks containing highlighted lines with class .remark-code-line-highlighted
(function(d) {
const hlines = d.querySelectorAll('.remark-code-line-highlighted');
const preParents = [];
const findPreParent = function(line, p = 0) {
if (p > 1) return null; // traverse up no further than grandparent
const el = line.parentElement;
return el.tagName === "PRE" ? el : findPreParent(el, ++p);
};
for (let line of hlines) {
let pre = findPreParent(line);
if (pre && !preParents.includes(pre)) preParents.push(pre);
}
preParents.forEach(p => p.classList.add("remark-code-has-line-highlighted"));
})(document);</script>
<script>
slideshow._releaseMath = function(el) {
var i, text, code, codes = el.getElementsByTagName('code');
for (i = 0; i < codes.length;) {
code = codes[i];
if (code.parentNode.tagName !== 'PRE' && code.childElementCount === 0) {
text = code.textContent;
if (/^\\\((.|\s)+\\\)$/.test(text) || /^\\\[(.|\s)+\\\]$/.test(text) ||
/^\$\$(.|\s)+\$\$$/.test(text) ||
/^\\begin\{([^}]+)\}(.|\s)+\\end\{[^}]+\}$/.test(text)) {
code.outerHTML = code.innerHTML; // remove <code></code>
continue;
}
}
i++;
}
};
slideshow._releaseMath(document);
</script>
<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
(function () {
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-MML-AM_CHTML';
if (location.protocol !== 'file:' && /^https?:/.test(script.src))
script.src = script.src.replace(/^https?:/, '');