forked from rstudio-education/stat545
-
Notifications
You must be signed in to change notification settings - Fork 0
/
13_date-times.Rmd
105 lines (63 loc) · 3.3 KB
/
13_date-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
# Dates and times {#date-time}
```{r include = FALSE}
source("common.R")
```
<!--JB: *Under development ... really just a placeholder / collection of links*-->
<!--Original content: https://stat545.com/block030_date-times.html-->
## Date-time vectors: where they fit in
We've spent a lot of time working with big, beautiful data frames. That are clean and wholesome, like the Gapminder data. With crude temporal information like, "THE YEAR IS 1952".
But real life will be much nastier. This information will come to you with much greater precision, reported to the last second or worse, complicated by time zones and daylight savings time idiosyncrasies. Or in some weird format.
Here we discuss common remedial tasks for dealing with date-times.
## Resources
I start with this because we cannot possibly do this topic justice in a short amount of time. Our goal is to make you aware of specific problems and solutions. Once you have a character problem in real life, these resources will be extremely helpful as you delve deeper.
[Dates and times][r4ds-dates-times] chapter from [R for Data Science][r4ds] by Hadley Wickham and Garrett Grolemund [-@wickham2016]. See also the subsection on dates and times in the [Data import chapter][r4ds-data-import].
The [lubridate][lubridate-web] package ([CRAN][lubridate-cran]; [GitHub][lubridate-github]; [main vignette][lubridate-vignette]).
Grolemund and Wickham's paper on lubridate in the Journal of Statistical Software [-@grolemund2011].
Exercises to push you to learn lubridate: [part 1][lubridate-ex1], [part 2][lubridate-ex2], and [part 3][lubridate-ex3] *posts include links to answers!*
## Load the tidyverse and lubridate
```{r start_dates, message = FALSE, warning = FALSE}
library(tidyverse)
library(lubridate)
```
## Get your hands on some dates or date-times
Use base `Sys.Date()` or lubridate's `today()` to get today's date, without any time.
```{r}
Sys.Date()
today()
```
They both give you something of class `Date`.
```{r}
str(Sys.Date())
class(Sys.Date())
str(today())
class(today())
```
Use base `Sys.time()` or lubridate's `now()` to get RIGHT NOW, meaning the date and the time.
```{r}
Sys.time()
now()
```
They both give you something of class `POSIXct` in R jargon.
```{r end_dates}
str(Sys.time())
class(Sys.time())
str(now())
class(now())
```
## Get date or date-time from character
One of the main ways dates and date-times come into your life:
<http://r4ds.had.co.nz/dates-and-times.html#creating-datetimes#from-strings>
## Build date or date-time from parts
Second most common way dates and date-times come into your life:
<http://r4ds.had.co.nz/dates-and-times.html#creating-datetimes#from-individual-components>
Once you have dates, you might want to edit them in a non-annoying way:
<http://r4ds.had.co.nz/dates-and-times.html#setting-components>
## Get parts from date or date-time
<http://r4ds.had.co.nz/dates-and-times.html#date-time-components#getting-components>
## Arithmetic with date or date-time
<http://r4ds.had.co.nz/dates-and-times.html#time-spans>
## Get character from date or date-time
Eventually you will need to print this stuff in, say, a report.
*I always use `format()` but assumed lubridate had something else/better. Am I missing something here? Probably. For now, read the help: `?format.POSIXct`.*
```{r links, child="links.md"}
```