-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy path17-dates_and_times.Rmd
219 lines (162 loc) · 5.87 KB
/
17-dates_and_times.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
# Dates and times
**Learning objectives:**
- **Create date** and **datetime** objects.
- Work with **datetime components.**
- Perform **arithmetic** on **timespans.**
- Recognize ways to deal with **timezones** in R.
```{r dates-and-times-libraries, warning=FALSE, message=FALSE}
library(tidyverse)
library(nycflights13)
```
## Date/time objects {-}
3 types of date/time objects:
- **date** = `<date>`
- **time** = `<time>`; use {hms} 📦
- **datetime** = `<dttm>` = "POSIXct"
- Always use simplest that works, but
- Beware of timezones!
## today() and now() {-}
```{r dates-and-times-now-today}
today() # just the date
now() # date with timestamp
```
## datetimes and read_csv() {-}
- Automatic: "YYYY-MM-DD HH:MM:SS"
- Else: specify `col_types` with `col_date()` and `col_datetime()`
- See ?readr::col_datetime for `format` options
- "%Y" = 4-digit year, "%B" = full month name, etc
- Specify `locale()` for non-English month names
## ymd_hms() and friends {-}
{lubridate} functions:
- `ymd_hms()` = "year month day hour minute second"
- Rearrange for other options
- Leave off `_hms` & `tz` for just date
```{r dates-and-times-ymd}
ymd_hms("2017-01-31 20:11:59")
```
## make_date() and make_datetime() {-}
```{r dates-and-times-make-datetime}
flights |>
select(year, month, day, hour, minute) |>
mutate(
departure = make_datetime(year, month, day, hour, minute),
dep_date = make_date(year, month, day)
)
```
## as_date() and as_datetime() {-}
- Most useful for offset from "Unix Epoch", 1970-01-01 00:00:00 UTC
- `as_date(days)`
- `as_datetime(seconds)`
```{r dates-and-times-as}
as_date(365 * 10 + 2) # 2 leap years
as_datetime(10 * 60 * 60)
```
## Getting components {-}
- `year()`, `month()`, `hour()`, `minute()`, `second()`
- `day()` == `mday()`, `yday()` (day of year), `wday()` (day of week)
```{r dates-and-times-components}
datetime <- ymd_hms("2024-03-08 19:32:28")
year(datetime)
mday(datetime)
yday(datetime)
wday(datetime, label = TRUE)
```
## Rounding datetimes {-}
```{r dates-and-times-rounding}
round_date(datetime, unit = "hour")
floor_date(datetime, unit = "hour")
ceiling_date(datetime, unit = "hour")
```
## Updating components {-}
```{r dates-and-times-updating}
year(datetime) <- 2030
datetime
hour(datetime) <- hour(datetime) + 1
datetime
update(datetime, years = 2024, months = 2)
update(datetime, years = 2024, months = 2, mdays = 30)
```
## Time spans {-}
- **Durations** = exact number of seconds
- **Periods** = human units (days, months, etc)
- **Intervals** = start and end datetime
## Durations {-}
```{r dates-and-times-durations}
jon_age <- today() - ymd("1975-01-24")
jon_age
as.duration(jon_age)
ddays(0:3) + dhours(2)
ymd_hms("2024-03-10 01:00:00", tz = "America/Chicago") + ddays(1)
dyears(1) / ddays(1)
```
## Periods {-}
```{r dates-and-times-periods}
class(days(1))
ymd_hms("2024-03-10 01:00:00", tz = "America/Chicago") + ddays(1)
ymd_hms("2024-03-10 01:00:00", tz = "America/Chicago") + days(1)
```
## Intervals {-}
```{r dates-and-times-intervals}
y2023 <- ymd("2023-01-01") %--% ymd("2024-01-01")
y2024 <- ymd("2024-01-01") %--% ymd("2025-01-01")
y2023 / dyears(1)
y2023 / days(1)
y2024 / dyears(1)
y2024 / days(1)
```
## Time zones {-}
```{r dates-and-times-timezones}
club_start_chicago <- ymd_hms("2024-03-08 13:00:00", tz = "America/Chicago")
force_tz(club_start_chicago, "Europe/Rome") # Only do this to fix mistakes
with_tz(club_start_chicago, "UTC")
with_tz(club_start_chicago, "Europe/Rome")
with_tz(club_start_chicago, "Australia/Sydney")
```
## Daylight saving time {-}
```{r dates-and-times-timezones-dst}
club_starts <- club_start_chicago + weeks(0:5)
club_starts |> hour()
club_starts |> with_tz("Europe/Rome") |> hour()
club_starts |> with_tz("UTC") |> hour() # 😢
club_starts |> with_tz("Australia/Sydney") |> hour() # 😢
```
## Lord Howe Island likes bugs {-}
![Lord Howe Island stick insect aka tree lobster](images/17-lord_howe_stick_insect.jpg)
## Lord Howe Island likes time zone bugs {-}
Lord Howe Island changes by 30 minutes for daylight saving time
```{r dates-and-times-timezones-howe}
club_starts |> with_tz("Australia/Sydney") |> hour()
club_starts |> with_tz("Australia/Lord_Howe") |> hour()
club_starts |> with_tz("Australia/Sydney") |> minute()
club_starts |> with_tz("Australia/Lord_Howe") |> minute()
```
## Meeting Videos
### Cohort 5
`r knitr::include_url("https://www.youtube.com/embed/J8QU4RBdi5c")`
<details>
<summary> Meeting chat log </summary>
```
00:17:21 Sandra Muroy: Hi Lucus!
00:17:52 lucus w: Hi Sandra
00:27:34 Ryan Metcalf: The “Visual Markdown Editor” icon Becki mentioned is intended to provide a similar experience to Jupyter Notebooks.
00:41:39 Federica Gazzelloni: round_date:: https://lubridate.tidyverse.org/reference/round_date.html
00:51:22 lucus w: Dates and time are HARD
00:51:40 Sandra Muroy: agree!
00:52:21 Ryan Metcalf: For a list of timezone abbreviations in R, run `OlsonNames(tzdir = NULL)`
00:53:16 Sandra Muroy: thanks Ryan!
00:58:51 Ryan Metcalf: I learned a trick from Federica….for syntax questions, use “?`<the character in question>`” Note the back ticks to highlight the sequence of character.
01:06:13 lucus w: “?” -- aka “what the”
01:07:42 Federica Gazzelloni: ?`%--%`
01:08:42 Federica Gazzelloni: start %--% end
01:10:08 Becki R. (she/her): Thanks, Federica! The easiest answer is the best.
01:10:54 lucus w: In healthcare there’s a tendency to deidentify date time objects as it’s considered patient identified information. Masking those date time randomly is a huge challenge
01:11:19 Sandra Muroy: interesting
01:13:57 Sandra Muroy: happy holidays everyone!
```
</details>
### Cohort 6
`r knitr::include_url("https://www.youtube.com/embed/URL")`
### Cohort 7
`r knitr::include_url("https://www.youtube.com/embed/pGbCWFB4Rz4")`
### Cohort 8
`r knitr::include_url("https://www.youtube.com/embed/QXFhZjc9S0o")`