forked from isi-avbulimen/R-cookbook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
04-dates.Rmd
411 lines (217 loc) · 9.15 KB
/
04-dates.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
```{r include=FALSE}
knitr::opts_chunk$set(collapse=TRUE, comment="#>")
```
# Working with dates and times {#dates-times}
```{r include=FALSE}
library(dplyr)
library(readr)
library(reshape2)
library(tidyr)
library(janitor)
library(lubridate)
# Read in required data using public data.gov extract
road_accidents <- readr::read_rds("data/road_accidents_2017.RDS")
# create character version of date to use in examples
road_accidents$Date1 <- as.character(road_accidents$Date)
```
This chapter provides an overview of working with dates and times, for example extracting year or month from a date, and converting characters to a date.
One of the main packages used to work with dates is **{lubridate}**.
More information can be found on the {lubridate} cheatsheet at the following link: https://www.rstudio.com/resources/cheatsheets/
Date vectors are just vectors of class double with an additional class attribute set as "Date".
```{r}
DfT_birthday <- lubridate::as_date("1919-08-14")
typeof(DfT_birthday)
attributes(DfT_birthday)
```
If we remove the class using `unclass()` we can reveal the value of the double, which is the number of days since "1970-01-01"^[a special date known as the Unix Epoch], since DfT's birthday is before this date, the double is negative.
```{r}
unclass(DfT_birthday)
```
This chapter will be using the road accident data set:
```{r, echo = FALSE, results='asis'}
knitr::kable(head(dplyr::select(road_accidents, acc_index, Date, Police_Force, Day_of_Week, Time)),
caption = "Reported Road Accidents, 2017")
```
## Working with dates
### Converting a character to a date
In R, dates can be converted to a specific date variable type in order to use the variable as a date.
Having a variable as a date means that you can:
- extract the different elements of the date (year, month etc.)
- calculate differences between dates
This can be done in the following way:
- Identify the order of the year, month, day and use the appropriate function (ymd, mdy, dmy etc.)
```{r, echo = TRUE}
# convert date to date object
# check class of date
class(road_accidents$Date1)
# look at the date variable and see what order it is in (year-m-d)
# therefore use the ymd function
road_accidents$Date1 <- lubridate::ymd(road_accidents$Date1)
# now check class
class(road_accidents$Date1)
```
### Get year from date
Use the **year** function from {lubridate}:
```{r}
road_accidents$Year <- lubridate::year(road_accidents$Date1)
```
*See Table 5.2 for output*
### Get month from date
Use the **month** function from {lubridate}:
```{r}
road_accidents$Month <- lubridate::month(road_accidents$Date1)
```
*See Table 5.2 for output*
### Get day from date
Use the **day** function from {lubridate}:
```{r}
road_accidents$Day <- lubridate::day(road_accidents$Date1)
```
*See Table 5.2 for output*
### Get weekday from date
Use the **wday** function from {lubridate} to get the weekday label:
```{r}
road_accidents$weekday <- lubridate::wday(road_accidents$Date1)
```
*See Table 5.2 for output*
### Get quarter from date
Use the **quarter** function from {lubridate}:
```{r}
road_accidents$Quarter <- lubridate::quarter(road_accidents$Date1)
```
*See Table 5.2 for output*
```{r, include = FALSE}
dates <- dplyr::select(road_accidents, Date1, Year, Quarter, Month, Day, weekday)
```
```{r, echo = FALSE, results='asis'}
knitr::kable(head(dates),
caption = "Using lubridate to extract time information")
```
### Find difference between two dates
```{r, include = FALSE}
# First create new date column so difference between two dates can be found
road_accidents$Date2 <-round_date(road_accidents$Date1, unit = "month")
```
```{r, echo = FALSE, results='asis'}
knitr::kable(head(dplyr::select(road_accidents, acc_index, Date1, Date2)),
caption = "Find difference between two dates")
```
Use the **as.duration** function to find the duration between two dates. The duration to be measured can be specified:
- dhours
- dweeks
- ddays
- dminutes
- dyears
To find out the number of days difference, the **as.duration** function calculates the duration in seconds so the duration must be divided by the desired duration (ddays) to convert to duration in days.
```{r}
road_accidents$date_diff <-
lubridate::as.duration(road_accidents$Date2 %--% road_accidents$Date1) / ddays(1)
```
```{r, echo = FALSE, results='asis'}
knitr::kable(head(dplyr::select(road_accidents, acc_index, Date1, Date2, date_diff)),
caption = "Find difference between two dates")
```
The `%--%` operator is used to define an **interval**. So, this code is calculating the duration of the interval between `Date2` and `Date1`.
The number after **ddays** indicates by how many units the duration is (i.e. one day).
### Convert month (integer to character)
{base} R has a useful function which takes the month numbers and converts them to the corresponding text.
```{r}
road_accidents$Month_lab <- month.abb[road_accidents$Month]
```
```{r, echo = FALSE, results='asis'}
knitr::kable(head(dplyr::select(road_accidents, acc_index, Date, Month, Month_lab)),
caption = "Convert month to character")
```
### Convert month (character to integer)
{base} R has a useful function which takes the month text and converts them to the corresponding number.
```{r}
road_accidents$Month <- match(road_accidents$Month_lab,month.abb)
```
```{r, echo = FALSE, results='asis'}
knitr::kable(head(dplyr::select(road_accidents, acc_index, Date, Month, Month_lab)),
caption = "Convert month character to integer")
```
### Merge separate date information into a date
The {lubridate} package can be used in conjunction with the paste function to combine columns separate date information (e.g. year, month, day) into one date variable.
```{r}
road_accidents$date <-
paste(road_accidents$Year, road_accidents$Month, road_accidents$Day, sep="-") %>%
ymd()%>%
as.Date()
```
```{r, echo = FALSE, results='asis'}
knitr::kable(head(dplyr::select(road_accidents, acc_index, Date, Year, Month, Day, date)),
caption = "Convert month to character")
```
## Working with date-times
A date-time stores date and time information.
### Converting a character to a date-time
This is similar to converting a character to a date as mentioned above.
This can be done in the following way:
- Identify the order of the year, month, day, and time elements (hour, minute and second or just hour and minute) and use the appropriate function (ymd, mdy, dmy etc.)
```{r, eval = FALSE}
# convert date to date object
# look at the date variable and see what order it is in (year-m-d, hms "2017-11-28 14:00)
# therefore use the ymd_hm
road_accidents$Date_time1 <- lubridate::ymd_hm(road_accidents$Date_time)
```
### Extract date from date time variable
Use the **date** function to extract the date from a date time variable.
The year/month/day information can then be extracted from the date using the code examples above.
```{r, eval = FALSE}
road_accidents$Date2 <- lubridate::date(road_accidents$Date_time)
```
### Convert character to hms (time) variable
Convert time as character into a hms variable so the variable can manipulated as a time object.
This can be done using the **{hms}** package.
```{r}
road_accidents$Time <- hms::as_hms(road_accidents$Time)
```
### Extract hour from time
Use the **hour** function from the {lubridate} package to extract hour information.
```{r}
road_accidents$hour <- lubridate::hour(road_accidents$Time)
```
*See Table 5.8 for output*
### Extract minute from time
Use the **minute** function from the {lubridate} package to extract minute information.
```{r}
road_accidents$minute <- lubridate::minute(road_accidents$Time)
```
*See Table 5.8 for output*
### Extract second from time
Use the **second** function from the {lubridate} package to extract second information.
```{r}
road_accidents$second <- lubridate::second(road_accidents$Time)
```
*See Table 5.8 for output*
```{r, echo = FALSE, results='asis'}
knitr::kable(head(dplyr::select(road_accidents, acc_index, Time, hour, minute, second)),
caption = "Extract time information")
```
### Merge separate time information into one variable
Hour, minute and second variables can be merged to create a time variable, and then converted to hms.
```{r}
# merge seperate time information
road_accidents$time2 <- paste(road_accidents$hour,road_accidents$minute, road_accidents$second, sep=":")
# convert to hms
road_accidents$time3 <- hms::as_hms(road_accidents$time2)
```
```{r, echo = FALSE, results='asis'}
knitr::kable(head(dplyr::select(road_accidents, acc_index, hour, minute, second, time3)),
caption = "Merge time information")
```
### find the difference between two times
Use the {base} r **difftime** function to find the difference between two times.
Note: this can also be used to find the difference in days or weeks.
Also note: the object must be hms/date to be able to calculate the difference.
```{r}
time_first <- hms::as.hms("11:00:00")
time_second <- hms::as.hms("11:05:00")
difference <- difftime(time_first, time_second, "mins" )
```
```{r, echo = TRUE }
difference
```
Change the unit of measurement to get different time differences (for days and weeks you'll need a date rather than a hms).
Units: "secs", "mins", "hours", "days", "weeks"