-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path05-date-manipulation.Rmd
108 lines (86 loc) · 3.14 KB
/
05-date-manipulation.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
# Working with Dates
## Overview
At some point in time (:p), you'll need to work with date data. This tutorial will illustrate concepts including: converting date formats, extracting date information (e.g., day of week, year), and calculate a date relative to another.
## Load libs
```{r}
library(tidyverse)
```
```{r}
# create date matrix
df <- expand.grid(month=seq(1,12,1),
day=seq(1,31,1),
year=seq(1930,2030)) %>%
mutate(mdy = lubridate::mdy(paste0(month,"/",day,"/",year))) %>%
mutate(dt_month = lubridate::month(mdy),
dt_day = lubridate::day(mdy),
dt_year = lubridate::year(mdy),
dt_week = lubridate::week(mdy),
dt_yday = lubridate::yday(mdy),
dt_weekday = lubridate::wday(mdy),
dt_weekday_l = lubridate::wday(mdy, label=T)) %>%
filter(!is.na(dt_weekday)) # remove feb 29, 30, 31
# export tables
knitr::kable(head(df)) %>% kableExtra::kable_classic()
knitr::kable(head(df)) %>% kableExtra::kable_material()
knitr::kable(head(df)) %>% kableExtra::kable_material_dark()
```
```{r}
# mark specific dates
df_marked <- df %>%
mutate(cinco_de_mayo_taco_tuesday = ifelse(dt_month == 5 &
dt_day == 5 &
dt_weekday_l == "Tue", 1, 0),
friday_13th = ifelse(dt_day == 13 &
dt_weekday_l == "Fri", 1, 0),
erica_bday = ifelse(dt_day == 4 &
dt_month == 4, 1, 0))
# segment days Alexa mentioned
all_cinco_de_mayo_taco_tuesday <- df_marked %>%
filter(cinco_de_mayo_taco_tuesday == 1)
all_friday13th <- df_marked %>%
filter(friday_13th == 1)
all_erica <- df_marked %>%
filter(erica_bday == 1) %>%
filter(year >= 1991)
```
## Count wife birthday by day of week
```{r}
erica_wkday <- all_erica %>%
group_by(dt_weekday_l) %>%
summarise(n = n())
```
## Plot time series of Cinco de Mayo landing on a Tuesday (Taco Tuesday!)
```{r}
ggplot(df_marked, aes(year, cinco_de_mayo_taco_tuesday)) +
geom_tile() +
theme_minimal()
ggplot(df_marked, aes(year, cinco_de_mayo_taco_tuesday)) +
geom_path() +
theme_minimal()
ggplot(erica_wkday, aes(dt_weekday_l, n, group=dt_weekday_l, fill=dt_weekday_l)) +
geom_bar(stat="identity")
```
## Calculating dates relative to other dates
:::attention
These operations work on types as.Date data types (and related as.POSIXct), not character data.
:::
```{r}
ref_date <- as.Date("2020-10-11")
paste0("Reference date: ", ref_date)
paste0("Reference date - 90 days: ", ref_date - 90)
paste0("Reference date + 90 days: ", ref_date + 90)
```
```{r}
all_ref_dates <- tibble(
"ref_labels" = c(
"Reference date - 90 days",
"Reference date + 90 days",
"Reference date - 5000 days",
"Reference date + 5000 days"),
"original_date" = c("2020-10-11"),
"ref_values" = c(ref_date - 90,
ref_date + 90,
ref_date - 5000,
ref_date + 5000))
knitr::kable(all_ref_dates) %>% kableExtra::kable_material_dark()
```