-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.Rmd
397 lines (228 loc) · 8.72 KB
/
index.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
---
title : Data Manipulation with R
author : Kaushik Roy Chowdhury
framework : deckjs
deckjs :
theme : swiss
highlighter : highlight.js # {highlight.js, prettify, highlight}
hitheme : googlecode #
widgets : [mathjax] # {mathjax, quiz, bootstrap}
mode : standalone # {standalone, draft}
knit : slidify::knit2slides
---
<style>
code {
fontsize: 12pt;
}
pre {
font-size: -1em;
}
</style>
# Import data into R
---
## Importing data into R
Conforming to its' philosophy of freedom (of choice), reading data into R can be performed in various ways.
### Reading data with Base R functions
* Widely used functions for reading data into R
* Available with base R distribution
* No packages required
* Can be slow and take up surprising amount of memory when reading large files
* `read.table()` and family
```{r setup1, include = FALSE}
library(knitr)
opts_chunk$set(comment = "", cache = FALSE, message=FALSE, tidy = FALSE)
```
__Usage__: `read.table(file, header = FALSE, sep = "", colClasses = NA, stringsAsFactors = TRUE, ...)`
---
## Is there a fast and efficient way to read-in data?
- `data.table()` package provides an alternative.
- `fread()` _fast file reader function_ is a fast and efficient way to read in data into R
__Usage__: `fread(input, ...)` where `...` takes in same arguments as that of `read.table`.
---
## Timing `read.table()` and `fread()` with a 20MB .csv file
The file `flights.csv` can be downloaded from [here](http://bit.ly/1L4IFxB)
```{r timing}
# install packages if not present
#install.packages(c("data.table", "rbenchmark"))
# load install packages
library(data.table); library(rbenchmark)
# file saved in windows default directory (~ = C:/Users/.../Documents)
read_base <- function(x) raw <- read.csv("~/flights.csv")
read_DT <- function(x) rawDT <<- fread("~/flights.csv")
# reading a 20MB .csv file
benchmark(read_base(), read_DT(), replications = 1,
columns = c("test", "elapsed"))
```
`fread()` is almost 5x as fast as `read.csv()`
---
## A look at the data
Having read the `flights.csv` data into R using `fread()` function, here are the first few rows
```{r look, echo=FALSE}
head(rawDT)
```
The data contains flights information of all planes that departed NYC (i.e. JFK, LGA or EWR airports) in 2013.
__Data Source__: [RITA, Bureau of transportation statistics](http://www.transtats.bts.gov/DL_SelectFields.asp?Table_ID=236)
---
# Data step of `data.table()`
---
## Deep-dive into `data.table()` package
`data.table()` provides __faster__ and __efficient__ manipulation of data stored in RAM
Perform the following operations:
- Filtering relevant data/rows
- Creating new variables, modify/delete columns
- Group by operations
- Summarize data
- Ordered joins
- Transpose data
__General form:__
> `DT[i, j, by]`: Take DT, subset rows using `i`, calculate `j` grouped by `by`
---
# Filter/Subset
---
## Filtering data
`DT[i, j, by]`: Take DT, subset rows using `i`,<span style="opacity: 0.15;"> calculate `j`, grouped by `by` </span>
Rows can be filtered using column names satisfying conditions
- Select all flights departing from JFK airport and has a departure delay of more than 30 minutes
```{r subset2i}
head(rawDT[origin == "JFK" & dep_delay > 30])
```
---
# Manipulating columns (adding/updating)
---
## Create new variables (standalone)
`DT[i, j, by]`: Take DT, <span style="opacity: 0.2;"> subset rows using `i`, </span> calculate `j` <span style="opacity: 0.2;"> grouped by `by` </span>
New variables can be created in the `j` argument of data table operation
- Calculate air speed for each flight (= distance/air_time)
```{r calc_speed}
head(rawDT[, .(air_speed = distance/air_time)])
```
- `.()` is an alias for `list()` to perform multiple operations separated by ','
- If `.()` is not used, the result is a vector, else the result is a `data.table`
---
## Adding new variables to `data.table`
To add the `air_speed` variable in the `rawDT data.table`, use `:=` operator
```{r calc_speed1}
head(rawDT[, air_speed := distance/air_time])
```
---
## Adding multiple new variables to `data.table`
- To add multiple variables, `air_speed` and `total_delay (=dep_delay + arr_delay)` use a chained operation
- Chaining operations improves readability and avoids intermediate assignments
```{r calc_speed2}
# in rawDT[, create var1][, create var2][print rows 1:3]
rawDT[, air_speed := distance/air_time][, total_delay := dep_delay + arr_delay][1:3]
```
---
# Group by operations
---
## Grouped Operations
`DT[i, j, by]`: Take DT, <span style="opacity: 0.2;"> subset rows using `i`, calculate `j` </span> grouped by `by`
- Calculate the average air speed for each carrier
- This can be achieved by calculating the air speed variable and take an average across all flights __grouped by__ carrier
- Make `na.rm = TRUE` which removes the `NA` (missing values) from the data when calculating the `mean`
```{r calc_speed3}
# calculate average air speed by carrier and print rows 1 to 5
rawDT[, .(avg_air_speed = mean(distance/air_time, na.rm = TRUE)), by = carrier][1:5]
```
---
# Summarize
---
## Data summary
Summarize data using necessary arguments of `i`, `j` and `by` of `data.table`
__Examples:__
- Calculate daily count of all flights departing from JFK airport (`origin == "JFK`)
- For all flights flying out of JFK airport (`origin == "JFK`), find the carrier with maximum average departure delay
- In which month of the year does flights departing from JFK airport has the maximum departure delay?
---
### Daily count of flights departing from JFK airport
```{r smry1}
head(rawDT[origin == "JFK", .N, by = day])
```
---
### Carrier with maximum average departure delay for flights departing from JFK airport
```{r smry2}
head(rawDT[origin == "JFK", .(avg_dep_delay = mean(dep_delay, na.rm = TRUE)), by = carrier][order(-avg_dep_delay)])
```
---
### Month with maximum average departure delay for flights departing from JFK airport
```{r smry3}
smryDT <- rawDT[origin == "JFK", .(avg_dep_delay = mean(dep_delay, na.rm = TRUE)), by = month]
# setorder works much faster than base::order from previous example
setorder(smryDT, -avg_dep_delay)
head(smryDT)
```
---
# Join/Merge
---
## Ordered Joins
Joins in `data.table` are performed using `merge.data.table()` function. However, `data.tables` need to be sorted by `key(s)` which are established using `setkey()` for an existing `data.table` or `key` argument while creating one
- Create two `data.tables` with same `key` value to join
```{r joins}
dt1 <- data.table(A = letters[1:10], X = 1:10, key = "A"); head(dt1)
dt2 <- data.table(A = letters[5:14], Y = 1:10, key = "A"); head(dt2)
```
---
### Left Outer Join
```{r loj}
# left outer join
merge.data.frame(x = dt1, y = dt2, all.x=TRUE)
```
---
### Right Inner Join
```{r rij}
# right inner join
merge.data.frame(x = dt2, y = dt1, all.x=FALSE)
```
---
### Full Outer Join
```{r foj}
# full outer join
merge.data.frame(x = dt1, y = dt2, all = TRUE)
```
---
# Transposing data
---
## Reshaping data `melt`
`data.tables` can be reshaped using the `melt` and `dcast` functions:
- __`melt`__: Wide-to-long (melting)
__Usage__: `melt(data, id.vars, measure.vars, variable.name = "variable", value.name = "value", ...)`
where,
`data` A `data.table` to melt
`id.vars` vector of id variables; if missing, all non-id columns are assigned
`measure.vars` vector of measure variables; if missing, all non-id columns are assigned
`variable.name` name for the measured variable names column
`value.name` name for the molten data values column
`...` advanced argument for `melt` functions
---
### Example
Create the data to melt
```{r melt1}
library(reshape2)
DT <- data.table(
i1 = c(1:3, NA),
i2 = c(5, 6, 7, 8),
f1 = c("A", "C", "D", "Q"),
c1 = c("XY", "FE", "AA", "GG"))
DT
```
---
### Melt the data
```{r melt2}
(DT.melt1 <- melt(DT, id = c("i1", "i2"), measure = c("f1", "c1")))
#rename variable and value columns
(DT.melt2 <- melt(DT, id = c("i1", "i2"), measure = c("f1", "c1"), variable.name = "Factors", value.name = "data_value"))
```
---
## Reshaping data `dcast`
- __`dcast.data.table`__: Long-to-wide (casting)
__Usage__: `dcast.data.table(data, formula, fun.aggregate = NULL, ...)`
where,
`data` A molten data.table
`formula` A formula of the form LHS ~ RHS to cast, eg: var1 + var2 ~ var3. The first varies slowest, and the last fastest. "..." represents all other variables not used in the formula and "." represents no variable
`fun.aggregate` Aggregation function needed if variables do not identify a single observation for each output cell
`...` other advanced arguments
---
### `dcast` the molten `data.table`
```{r dcast1}
(DT.dcast <- dcast.data.table(DT.melt2, i1+i2~Factors))
```