forked from awunderground/data-science-for-public-policy2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path02_advanced-data-cleaning.qmd
1047 lines (702 loc) · 27.8 KB
/
02_advanced-data-cleaning.qmd
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
---
title: "Advanced Data Cleaning"
abstract: "This section covers advanced techniques for cleaning and manipulating data."
format:
html:
toc: true
embed-resources: true
code-line-numbers: true
editor_options:
chunk_output_type: console
---
```{r}
#| echo: false
#| warning: false
library(tidyverse)
theme_set(theme_minimal())
```
```{r}
#| echo: false
exercise_number <- 1
```
## Review {#sec-review2}
R for Data Science (2e) [displays](https://r4ds.hadley.nz/intro.html#fig-ds-diagram) the first steps of the data science process as "Import", "Tidy", and "Transform". DSPP1 introduced important techniques for importing data like `read_csv()` and querying web APIs, for tidying data like `pivot_longer()`, and for transforming data like `mutate()`.
::: callout
#### [`r paste("Exercise", exercise_number)`]{style="color:#1696d2;"}
```{r}
#| echo: false
exercise_number <- exercise_number + 1
```
1. Use `mutate()` and `case_when()` to add a new variable called `speed_cat` to `cars` where the values are `"slow"` when `speed < 10`, `"moderate"` when `speed < 20`, and `"fast"` otherwise.
:::
## Import {#sec-import}
### `library(here)`
Developing Quarto documents in subdirectories is a pain. When interactively running code in the console, file paths are read as if the `.qmd` file is in the same folder as the `.Rproj`. When clicking render, paths are treated as if they are in the subdirectory where the `.qmd` file is.
[`library(here)`](https://here.r-lib.org/) resolves headaches around file referencing in [project-oriented workflows](https://www.tidyverse.org/blog/2017/12/workflow-vs-script/).
Loading `library(here)` will print your working directory.
```{r}
library(here)
```
After this, `here()` will use reasonable heuristics to find project files using relative file paths. When placing Quarto documents in a directory below the top-level directory, use `here()` and treat each folder and file as a different string.
**Before**
```{r}
#| eval: false
read_csv("data/raw/important-data.csv")
```
**After**
```{r}
#| eval: false
read_csv(here("data", "raw", "important-data.csv"))
```
### `library(readxl)`
We will focus on reading data from Excel workbooks. Excel is a bad tool with bad design that has led to many analytical errors. Unfortunately, it's a dominant tool for storing data and often enters the data science workflow.
`library(readxl)` is the premier package for reading data from `.xls` and `.xlsx` files. `read_excel()`, which works like `read_csv()`, loads data from `.xls` and `.xlsx` files. Consider data from the Urban Institute's [Debt in America](https://apps.urban.org/features/debt-interactive-map/?type=overall&variable=totcoll) feature accessed through the [Urban Institute Data Catalog](https://datacatalog.urban.org/dataset/debt-america-2022).
```{r}
library(readxl)
read_excel(here("data", "state_dia_delinquency_ 7 Jun 2022.xlsx"))
```
`read_excel()` has several useful arguments:
- `sheet` selects the sheet to read.
- `range` selects the cells to read and can use Excel-style ranges like "C34:D50".
- `skip` skips the selected number of rows.
- `n_max` selects the maximum number of rows to read.
Excel encourages bad habits and untidy data, so these arguments are useful for extracting data from messy Excel workbooks.
`readxl_example()` contains a perfect example. The workbook contains two sheets, which we can see with `excel_sheets()`.
```{r}
readxl_example("clippy.xlsx") |>
excel_sheets()
```
As is common with many Excel workbooks, the second sheet contains a second row of column names with parenthetical comments about each column.[^02_advanced-data-cleaning-1]
[^02_advanced-data-cleaning-1]: The instinct to include these comments is good. The execution is poor because it creates big headaches for people using programming languages. I suggest using a data dictionary instead.
```{r}
readxl_example("clippy.xlsx") |>
read_excel(sheet = "two-row-header")
```
[This vignette](https://readxl.tidyverse.org/articles/multiple-header-rows.html) suggests a simple solution to this problem.
```{r}
# extract the column names
col_names <- readxl_example("clippy.xlsx") |>
read_excel(sheet = "two-row-header", n_max = 0) |>
names()
# load the data and add the column names
readxl_example("clippy.xlsx") |>
read_excel(
sheet = "two-row-header",
skip = 2,
col_names = col_names
)
```
::: callout
#### [`r paste("Exercise", exercise_number)`]{style="color:#1696d2;"}
```{r}
#| echo: false
exercise_number <- exercise_number + 1
```
The [IRS Statistics of Income Division](https://www.irs.gov/statistics/soi-tax-stats-statistics-of-income) is one of the US's 13 principal statistical agencies. They publish rich information derived from tax returns. We will focus on Table 1, [Adjusted Gross Income (AGI) percentiles by state](https://www.irs.gov/statistics/soi-tax-stats-adjusted-gross-income-agi-percentile-data-by-state).
1. Read in the 52 cells in the first column that contain "United States", all 50 states, and the "District of Columbia".
2. Identify the cells containing data for "Adjusted gross income floor on percentiles". Read in the data with `read_excel()`. Either programmatically read in the column names (i.e. "Top 1 Percent", ...) or assign them with `col_names()`.
3. Use `bind_cols()` to combine the data from step 1 and step 2.
:::
```{r}
#| echo: false
#| eval: false
states <- read_excel(
here("data", "20in01stateshares.xlsx"),
range = "A7:A58",
col_names = "state"
)
names <- read_excel(
here("data", "20in01stateshares.xlsx"),
range = "J4:O4"
) |>
names()
data <- read_excel(
here("data", "20in01stateshares.xlsx"),
range = "J7:O58",
skip = 5,
col_names = names
)
data <- bind_cols(states, data)
```
[`library(tidyxl)`](https://cran.r-project.org/web/packages/tidyxl/index.html) contains tools for working with messy Excel workbooks, [`library(openxlsx)`](https://cran.r-project.org/web/packages/openxlsx/index.html) contains tools for creating Excel workbooks with R, and [`library(googlesheets4)`](https://cran.r-project.org/web/packages/openxlsx/index.html) contains tools for working with Google Sheets.
## Tidy {#sec-tidy}
The defining opinion of the tidyverse is its wholehearted adoption of tidy data. [Tidy data has three features](https://r4ds.hadley.nz/data-tidy#fig-tidy-structure):
1. Each variable forms a column.
2. Each observation forms a row.
3. Each type of observational unit forms a dataframe.
> Tidy datasets are all alike, but every messy dataset is messy in its own way. ~ [Hadley Wickham](https://r4ds.had.co.nz/tidy-data.html)
`library(tidyr)` is the main package for tidying untidy data. We'll practice some skills using examples from three workbooks from the [IRS SOI](https://www.irs.gov/statistics/soi-tax-stats-adjusted-gross-income-agi-percentile-data-by-state).
`pivot_longer()` is commonly used for tidying data and for making data longer for `library(ggplot2)`. `pivot_longer()` reorients data so that key-value pairs expressed as column name-column value are column value-column value in adjacent columns. `pivot_longer()` has three essential arguments:
1. `cols` is a vector of columns to pivot (or not pivot).
2. `names_to` is a string for the name of the column where the old column names will go (i.e. "series" in the figure).
3. `values_to` is a string for the name of the column where the values will go (i.e. "rate" in the figure).
```{r}
#| echo: false
knitr::include_graphics(here("images", "pivoting.png"))
```
`pivot_wider()` is the inverse of `pivot_longer()`.
#### Tidying Example 1
::: {.panel-tabset}
#### Untidy
**Why aren't the data tidy?**
```{r}
table1 <- tribble(
~state, ~agi2006, ~agi2016, ~agi2020,
"Alabama", 95067, 114510, 138244,
"Alaska", 17458, 23645, 26445,
"Arizona", 146307, 181691, 245258
)
table1
```
#### Cleaned
Year is a variable. This data is untidy because year is included in the column names.
```{r}
table1 <- tribble(
~state, ~agi2006, ~agi2016, ~agi2020,
"Alabama", 95067, 114510, 138244,
"Alaska", 17458, 23645, 26445,
"Arizona", 146307, 181691, 245258
)
table1
pivot_longer(
data = table1,
cols = -state,
names_to = "year",
values_to = "agi"
)
```
The `year` column isn't useful yet. We'll fix that later.
:::
<br>
`library(tidyr)` contains several functions to split values into multiple cells.
- `separate_wider_delim()` separates a value based on a delimeter and creates wider data.
- `separate_wider_position()` separates a value based on position and creates wider data.
- `separate_longer_delim()` separates a value based on a delimeter and creates longer data.
- `separate_longer_position()` separates a value based on position and creates longer data.
#### Tidying Example 2
::: {.panel-tabset}
#### Untidy
**Why aren't the data tidy?**
```{r}
table2 <- tribble(
~state, ~`agi2006|2016|2020`,
"Alabama", "95067|114510|138244",
"Alaska", "17458|23645|26445",
"Arizona", "146307|181691|245258"
)
table2
```
#### Cleaned
The values for 2006, 2016, and 2020 are all squished into one cell.
```{r}
table2 <- tribble(
~state, ~`agi2006|2016|2020`,
"Alabama", "95067|114510|138244",
"Alaska", "17458|23645|26445",
"Arizona", "146307|181691|245258"
)
table2
separate_wider_delim(
data = table2,
cols = `agi2006|2016|2020`,
delim = "|",
names = c("2006", "2016", "2020")
) |>
pivot_longer(
cols = -state,
names_to = "year",
values_to = "agi"
)
```
:::
<br>
`bind_rows()` combines data frames by stacking the rows.
```{r}
one <- tribble(
~id, ~var,
"1", 3.14,
"2", 3.15,
)
two <- tribble(
~id, ~var,
"3", 3.16,
"4", 3.17,
)
bind_rows(one, two)
```
`bind_cols()` combines data frames by appending columns.
```{r}
three <- tribble(
~id, ~var1,
"1", 3.14,
"2", 3.15,
)
four <- tribble(
~id, ~var2,
"1", 3.16,
"2", 3.17,
)
bind_cols(three, four)
```
When possible, we recommend using relational joins like `left_join()` to combine by columns because it is easy to miss-align rows with `bind_cols()`.
```{r}
left_join(
x = three,
y = four,
by = "id"
)
```
#### Tidying Example 3
::: {.panel-tabset}
#### Untidy
**Why aren't the data tidy?**
```{r}
table3_2006 <- tribble(
~state, ~agi,
"Alabama", "95067",
"Alaska", "17458",
"Arizona", "146307"
)
table3_2006
table3_2016 <- tribble(
~state, ~agi,
"Alabama", "114510",
"Alaska", "23645",
"Arizona", "181691"
)
table3_2016
table3_2020 <- tribble(
~state, ~`agi`,
"Alabama", "138244",
"Alaska", "26445",
"Arizona", "245258"
)
table3_2020
```
#### Cleaned
The variable year is contained in the data set names. The `.id` argument in `bind_rows()` allows us to create the year variable.
```{r}
table3_2006 <- tribble(
~state, ~agi,
"Alabama", 95067,
"Alaska", 17458,
"Arizona", 146307
)
table3_2006
table3_2016 <- tribble(
~state, ~agi,
"Alabama", 114510,
"Alaska", 23645,
"Arizona", 181691
)
table3_2016
table3_2020 <- tribble(
~state, ~`agi`,
"Alabama", 138244,
"Alaska", 26445,
"Arizona", 245258
)
table3_2020
bind_rows(
`2006` = table3_2006,
`2016` = table3_2016,
`2020` = table3_2020,
.id = "year"
)
```
:::
<br>
Relational joins are fundamental to working with tidy data. Tidy data can only contain one unit of observation (e.g. county or state not county and state). When data exist on multiple levels, they must be stored in separate tables that can later be combined.
::: {.callout-tip}
## Mutating Joins
Mutating joins add new variables to a data frame by matching observations from one data frame to observations in another data frame.
:::
::: {.callout-tip}
## Filtering Joins
Filtering joins drop observations based on the presence of their key (identifier) in another data frame.
For example, we may have a list of students in detention and a list of all students. We can use a filtering join to create a list of student not in detention.
:::
For now, we will focus on mutating joins. Let their be two data frames `x` and `y` and let both data frames have a key variable that uniquely identifies rows.
* `left_join(x, y)` appends variables from `y` on to `x` but only keeps observations from `x`.
* `full_join(x, y)` appends variables from `y` on to `x` and keeps all observations from `x` and `y`.
* `anti_join(x, y)` returns all observations from `x` without a match in `y`. `anti_join()` is traditionally only used for filtering joins, but it is useful for writing tests for mutating joins.
To learn more, read the [Joins chapter of R for Data Science (2e)](https://r4ds.hadley.nz/joins.html). [`library(tidylog)`](https://github.com/elbersb/tidylog) is a useful function for monitoring the behavior of joins.
#### Tidying Example 4
::: {.panel-tabset}
#### Untidy
**Why aren't the data tidy?**
```{r}
table4a <- tribble(
~state, ~agi,
"Alabama", 95067,
"Alaska", 17458,
"Arizona", 146307
)
table4a
table4b <- tribble(
~state, ~returns,
"Alabama", 1929941,
"Alaska", 322369,
"Arizona", 2454951
)
table4b
```
#### Cleaned
These data are tidy! But keeping the data in two separate data frames may not make sense. Let's use `full_join()` to combine the data and `anti_join()` to see if there are mismatches.
```{r}
table4a <- tribble(
~state, ~agi,
"Alabama", 95067,
"Alaska", 17458,
"Arizona", 146307
)
table4a
table4b <- tribble(
~state, ~returns,
"Alabama", 1929941,
"Alaska", 322369,
"Arizona", 2454951
)
table4b
full_join(table4a, table4b, by = "state")
anti_join(table4a, table4b, by = "state")
anti_join(table4b, table4a, by = "state")
```
:::
::: callout
#### [`r paste("Exercise", exercise_number)`]{style="color:#1696d2;"}
```{r}
#| echo: false
exercise_number <- exercise_number + 1
```
1. Use `pivot_longer()` to make the SOI percentile data from the earlier exercise longer. After the transformation, there should be one row per percentile per state.
:::
To see more examples, read the [tidy data section in R for Data Science (2e)](https://r4ds.hadley.nz/data-tidy.html)
## Transform {#sec-transform}
### Strings
Check out the [stringr cheat sheet](https://evoldyn.gitlab.io/evomics-2018/ref-sheets/R_strings.pdf).
`library(stringr)` contains powerful functions for working with strings in R. In data analysis, we may need to detect matches, subset strings, work with the lengths of strings, modify strings, and join and split strings.
#### Detecting Matches
`str_detect()` is useful for **detecting matches** in strings, which can be useful with `filter()`. Consider the executive orders data set and suppose we want to return executive orders that contain the word `"Virginia"`.
```{r}
eos <- read_csv(here("data", "executive-orders.csv")) |>
filter(!is.na(text)) |>
group_by(executive_order_number) |>
summarize(text = list(text)) |>
mutate(text = map_chr(text, ~paste(.x, collapse = " ")))
eos
eos |>
filter(str_detect(string = text, pattern = "Virginia"))
```
#### Subsetting Strings
`str_sub()` can **subset strings** based on positions within the string. Consider an example where we want to extract state FIPS codes from county FIPS codes.
```{r}
tibble(fips = c("01001", "02013", "04001")) |>
mutate(state_fips = str_sub(fips, start = 1, end = 2))
```
#### Managing Lengths
`str_pad()` is useful for **managing lengths**. Consider the common situation when zeros are dropped from the beginning of FIPS codes.
```{r}
tibble(fips = c(1, 2, 4)) |>
mutate(fips = str_pad(fips, side = "left", pad = "0", width = 2))
```
#### Modifying Strings
`str_replace()`, `str_replace_all()`, `str_remove()`, and `str_remove_all()` can delete or **modify** parts of strings. Consider an example where we have course names and we want to delete everything except numeric digits.[^regex]
[^regex]: This example uses regular expressions (regex). Visit [R4DS (2e)](https://r4ds.hadley.nz/regexps) for a review of regex.
```{r}
tibble(course = c("PPOL 670", "GOVT 8009", "PPOL 6819")) |>
mutate(course = str_remove(course, pattern = "[:alpha:]*\\s"))
```
`str_c()` and `str_glue()` are useful for **joining** strings. Consider an example where we want to "fill in the blank" with a variable in a data frame.
```{r}
tibble(fruit = c("apple", "banana", "cantelope")) |>
mutate(sentence = str_glue("my favorite fruit is {fruit}"))
```
```{r}
tibble(fruit = c("apple", "banana", "cantelope")) |>
mutate(
another_sentence =
str_c("Who doesn't like a good ", fruit, ".")
)
```
This workflow is useful for building up URLs when accessing APIs, scraping information from the Internet, and downloading many files.
::: callout
#### [`r paste("Exercise", exercise_number)`]{style="color:#1696d2;"}
```{r}
#| echo: false
exercise_number <- exercise_number + 1
```
1. Use `mutate()` and `library(stringr)` to create a variable for `year` from the earlier SOI exercise. For instance, `"agi2006"` should be `"2006"`.
2. Use `as.numeric()` to convert the string from step 1 into a numeric value.
3. Create a data visualization with `year` on the x-axis.
:::
### Factors
Check out the [forcats cheat sheet](https://forcats.tidyverse.org/).
Much of our work focuses on four of the six types of atomic vectors: logical, integer, double, and character. R also contains [augmented vectors](https://r4ds.had.co.nz/vectors.html#augmented-vectors) like factors.
Factors are categorical data stored as integers with a levels attribute. Character vectors often work well for categorical data and many of R's functions convert character vectors to factors. This happens with `lm()`.
Factors have many applications:
1. Giving the levels of a categorical variable non-alpha numeric order in a ggplot2 data visualization.
2. Running calculations on data with empty groups.
3. Representing categorical outcome variables in classification models.
#### Factor Basics
```{r}
x1 <- factor(c("a", "a", "b", "c"), levels = c("d", "c", "b", "a"))
x1
attributes(x1)
levels(x1)
```
`x1` has order but it isn't ordinal. Sometimes we'll come across ordinal factor variables, like with the `diamonds` data set. Unintentional ordinal variables can cause unexpected errors. For example, including ordinal data as predictors in regression models will lead to different estimated coefficients than other variable types.
```{r}
glimpse(diamonds)
```
```{r}
x2 <- factor(
c("a", "a", "b", "c"),
levels = c("d", "c", "b", "a"),
ordered = TRUE
)
x2
attributes(x2)
levels(x2)
```
@fig-factors shows how we can use a factor to give a variable a non-alpha numeric order and preserve empty levels. In this case, February and March have zero tropical depressions, tropical storms, and hurricanes and we want to demonstrate that emptiness.
```{r}
#| label: fig-factors
#| fig-cap: "Hurricane Season Peaks in Late Summer and Early Fall"
#| fig-subcap:
#| - "Figure without a factor"
#| - "Figure with a factor"
#| layout-ncol: 2
# use case_match to convert integers into month names
storms <- storms |>
mutate(
month = case_match(
month,
1 ~ "Jan",
4 ~ "Apr",
5 ~ "May",
6 ~ "Jun",
7 ~ "Jul",
8 ~ "Aug",
9 ~ "Sep",
10 ~ "Oct",
11 ~ "Nov",
12 ~ "Dec"
)
)
# create data viz without factors
storms |>
count(month) |>
ggplot(aes(x = n, y = month)) +
geom_col()
# add factor variable
months <- c("Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
storms <- storms |>
mutate(month = factor(month, levels = months))
# create data viz with factors
storms |>
count(month, .drop = FALSE) |>
ggplot(aes(x = n, y = month)) +
geom_col()
```
Factors also change the behavior of summary functions like `count()`.
```{r}
storms |>
count(month)
storms |>
count(month, .drop = FALSE)
```
`library(forcats)` simplifies many common operations on factor vectors.
#### Changing Order
`fct_relevel()`, `fct_rev()`, and `fct_reorder()` are useful functions for modifying the order of factor variables. @fig-fct_rev demonstrates using `fct_rev()` to flip the order of a categorical axis in ggplot2.
```{r}
#| label: fig-fct_rev
#| fig-cap: "Hurricane Season Peaks in Late Summer and Early Fall"
#| fig-subcap:
#| - "Descending"
#| - "Ascending"
#| layout-ncol: 2
storms |>
count(month, .drop = FALSE) |>
ggplot(aes(x = n, y = month)) +
geom_col()
storms |>
mutate(month = fct_rev(month)) |>
count(month, .drop = FALSE) |>
ggplot(aes(x = n, y = month)) +
geom_col()
```
@fig-fct_reorder orders the factor variable based on the number of observations in each category using `fct_reorder()`. `fct_reorder()` can order variables based on more sophisticated summaries than just magnitude. For example, it can order box-and-whisker plots based on the median or even something as arbitrary at the 60th percentile.
```{r}
#| label: fig-fct_reorder
#| fig-cap: "Hurricane Season Peaks in Late Summer and Early Fall"
#| fig-subcap:
#| - "Alpha-numeric"
#| - "Magnitude"
#| layout-ncol: 2
storms |>
count(month, .drop = FALSE) |>
ggplot(aes(x = n, y = month)) +
geom_col()
storms |>
count(month, .drop = FALSE) |>
mutate(month = fct_reorder(.f = month, .x = n, .fun = median)) |>
ggplot(aes(x = n, y = month)) +
geom_col()
```
#### Changing Values
Functions like `fct_recode()` and `fct_lump_min()` are useful for changing factor variables. @fig-fct_lump_min combines categories with fewer than 1,000 observations into an `"Other"` group.
```{r}
#| label: fig-fct_lump_min
#| fig-cap: "Hurricane Season Peaks in Late Summer and Early Fall"
#| fig-subcap:
#| - "All"
#| - "Lumped"
#| layout-ncol: 2
storms |>
count(month, .drop = FALSE) |>
ggplot(aes(x = n, y = month)) +
geom_col()
storms |>
mutate(month = fct_lump_min(month, min = 1000)) |>
count(month, .drop = FALSE) |>
ggplot(aes(x = n, y = month)) +
geom_col()
```
### Dates and Date-Times
Check out the [lubridate cheat sheet](https://evoldyn.gitlab.io/evomics-2018/ref-sheets/R_lubridate.pdf).
There are many ways to store dates.
* March 14, 1992
* 03/14/1992
* 14/03/1992
* 14th of March '92
One way of storing dates is the **best**. The [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) date format is an international standard with appealing properties like fixed lengths and self ordering. The format is `YYYY-MM-DD`.
`library(lubridate)` has useful functions that will take dates of any format and convert them to the ISO 8601 standard.
```{r}
library(lubridate)
mdy("March 14, 1992")
mdy("03/14/1992")
dmy("14/03/1992")
dmy("14th of March '92")
```
These functions return variables of class `"Date"`.
```{r}
class(mdy("March 14, 1992"))
```
`library(lubridate)` also contains functions for parsing date times into ISO 8601 standard. Times are slightly trickier because of time zones.
```{r}
mdy_hms("12/02/2021 1:00:00")
mdy_hms("12/02/2021 1:00:00", tz = "EST")
mdy_hms("12/02/2021 1:00:00", tz = "America/Chicago")
```
By default, `library(lubridate)` will put the date times in Coordinated Universal Time (UTC), which is the successor to Greenwich Mean Time (GMT). I recommend carefully reading the data dictionary if time zones are important for your analysis or if your data cross time zones. This is especially important during time changes (e.g. "spring forward" and "fall back").
Fortunately, if you encode your dates or date-times correctly, then `library(lubridate)` will automatically account for time changes, time zones, leap years, leap seconds, and all of the quirks of dates and times.
::: callout
#### [`r paste("Exercise", exercise_number)`]{style="color:#1696d2;"}
```{r}
#| echo: false
exercise_number <- exercise_number + 1
```
```{r}
dates <- tribble(
~date,
"12/01/1987",
"12/02/1987",
"12/03/1987"
)
```
1. Create the `dates` data from above with `tribble()`.
2. Use `mutate()` to convert the `date` column to the ISO 8601 standard (YYYY-MM-DD).
:::
#### Extracting Components
`library(lubridate)` contains functions for extracting components from dates like the year, month, day, and weekday. Conisder the follow data set about [full moons](https://www.timeanddate.com/moon/phases/usa/washington-dc) in Washington, DC in 2023.
```{r}
full_moons <- tribble(
~full_moon,
"2023-01-06",
"2023-02-05",
"2023-03-07",
"2023-04-06",
"2023-05-05",
"2023-06-03",
"2023-07-03",
"2023-08-01",
"2023-08-30",
"2023-09-29",
"2023-10-28",
"2023-11-27",
"2023-12-26"
) |>
mutate(full_moon = as_date(full_moon))
```
Suppose we want to know the weekday of each full moon.
```{r}
full_moons |>
mutate(week_day = wday(full_moon, label = TRUE))
```
#### Math
`library(lubridate)` easily handles math with dates and date-times. Suppose we want to calculate the number of days since American Independence Day:
```{r}
today() - as_date("1776-07-04")
```
In this case, subtraction creates an object of class `difftime` represented in days. We can use the `difftimes()` function to calculate differences in other units.
```{r}
difftime(today(), as_date("1776-07-04"), units = "mins")
```
#### Periods
**Periods** track clock time or a calendar time. We use periods when we set a recurring meetings on a calendar and when we set an alarm to wake up in the morning.
This can lead to some interesting results. Do we always add 365 days when we add 1 year to a date? With periods, this isn't true. Sometimes we add 366 days during leap years. For example,
```{r}
start <- as_date("1999-03-14")
end <- start + years(1)
end
end - start
```
#### Durations
**Durations** track the passage of physical time in exact seconds. Durations are like sand falling into an hourglass. Duration functions start with `d` like `dyears()` and `dminutes()`.
```{r}
start <- as_date("1999-03-14")
end <- start + dyears(1)
end
```
Now we always add 365 days, but we see that March 13th is one year after March 14th.
#### Intervals
Until now, we've focused on points in time. **Intervals** have length and have a starting point and an ending point.
Suppose classes start on August 23rd and proceed every week for a while. Do any of these dates conflict with Georgetown's fall break?
```{r}
classes <- as_date("2023-08-23") + weeks(0:15)
fall_break <- interval(as_date("2023-11-22"), as_date("2023-11-26"))
classes %within% fall_break
```
We focused on dates, but many of the same principles hold for date-times.
::: callout
#### [`r paste("Exercise", exercise_number)`]{style="color:#1696d2;"}