forked from rdpeng/CourseraLectures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDates.Rmd
132 lines (93 loc) · 2.74 KB
/
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
% Dates and Times in R
% Computing for Data Analysis
%
```{r, echo=FALSE}
options(width = 50)
```
# Dates and Times in R
R has developed a special representation of dates and times
- Dates are represented by the `Date` class
- Times are represented by the `POSIXct` or the `POSIXlt` class
- Dates are stored internally as the number of days since 1970-01-01
- Times are stored internally as the number of seconds since
1970-01-01
# Dates in R
Dates are represented by the `Date` class and can be coerced from a
character string using the `as.Date()` function.
```{r}
x <- as.Date("1970-01-01")
x
unclass(x)
unclass(as.Date("1970-01-02"))
```
# Times in R
Times are represented using the `POSIXct` or the `POSIXlt` class
- `POSIXct` is just a very large integer under the hood; it use a
useful class when you want to store times in something like a data
frame
- `POSIXlt` is a list underneath and it stores a bunch of other useful
information like the day of the week, day of the year, month, day of
the month
There are a number of generic functions that work on dates and times
- `weekdays`: give the day of the week
- `months`: give the month name
- `quarters`: give the quarter number ("Q1", "Q2", "Q3", or "Q4")
# Times in R
Times can be coerced from a character string using the `as.POSIXlt`
or `as.POSIXct` function.
```{r}
x <- Sys.time()
x
p <- as.POSIXlt(x)
names(unclass(p))
p$sec
```
# Times in R
You can also use the `POSIXct` format.
```{r}
x <- Sys.time()
x ## Already in `POSIXct' format
unclass(x)
x$sec
p <- as.POSIXlt(x)
p$sec
```
# Times in R
Finally, there is the `strptime` function in case your dates are
written in a different format
```{r}
datestring <- c("January 10, 2012 10:40", "December 9, 2011 9:10")
x <- strptime(datestring, "%B %d, %Y %H:%M")
x
class(x)
```
I can *never* remember the formatting strings. Check `?strptime` for
details.
# Operations on Dates and Times
You can use mathematical operations on dates and times. Well, really
just `+` and `-`. You can do comparisons too (i.e. `==`, `<=`)
```{r}
x <- as.Date("2012-01-01")
y <- strptime("9 Jan 2011 11:34:21", "%d %b %Y %H:%M:%S")
x - y
x <- as.POSIXlt(x)
x - y
```
# Operations on Dates and Times
Even keeps track of leap years, leap seconds, daylight savings, and
time zones.
```{r}
x <- as.Date("2012-03-01")
y <- as.Date("2012-02-28")
x - y
x <- as.POSIXct("2012-10-25 01:00:00")
y <- as.POSIXct("2012-10-25 06:00:00", tz = "GMT")
y - x
```
# Summary
- Dates and times have special classes in R that allow for numerical
and statistical calculations
- Dates use the `Date` class
- Times use the `POSIXct` and `POSIXlt` class
- Character strings can be coerced to Date/Time classes using the
`strptime` function or the `as.Date`, `as.POSIXlt`, or `as.POSIXct`