This repository has been archived by the owner on Jan 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
/
03_DataWrangling.Rmd
324 lines (231 loc) · 7.71 KB
/
03_DataWrangling.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
---
title : "Data Wrangling 1 "
---
```{r, echo=FALSE, message=FALSE, results='hide', purl=FALSE}
source("knitr_header.R")
```
<div>
<object data="presentations/03_DataWrangling.pdf" type="application/pdf" width="100%" height="600px">
<p>It appears you don't have a PDF plugin for this browser.
No biggie... you can <a href="presentations/03_DataWrangling.pdf">click here to
download the PDF file.</a></p>
</object>
</div>
<p><a href="presentations/03_DataWrangling.pdf">Download the PDF of the presentation</a></p>
[<i class="fa fa-file-code-o fa-3x" aria-hidden="true"></i> The R Script associated with this page is available here](`r output`). Download this file and open it (or copy-paste into a new script) with RStudio so you can follow along.
# RStudio Shortcuts
## Running code
* `ctrl-R` (or `command-R`) to run current line
* Highlight `code` in script and run `ctrl-R` (or `command-R`) to run selection
* Buttons: <img src="03_assets/Source.png" style="width: 25%"/>
## Switching windows
* `ctrl-1`: script window
* `ctrl-2`: console window
> Try to run today's script without using your mouse/trackpad
# Data wrangling
## Useful packages: [`tidyverse`](https://www.tidyverse.org/packages/)
[Cheat sheets on website](https://www.rstudio.com/resources/cheatsheets/) for [Data Wrangling](https://www.rstudio.com/wp-content/uploads/2015/02/data-wrangling-cheatsheet.pdf)
```{r,results='hide', message=FALSE, warning=F}
library(tidyverse)
```
Remember use `install.packages("tidyverse")` to install a new package.
### Example operations from [here](https://cran.rstudio.com/web/packages/dplyr/vignettes/introduction.html)
## New York City Flights
Data from [US Bureau of Transportation Statistics](http://www.transtats.bts.gov/DatabaseInfo.asp?DB_ID=120&Link=0) (see `?nycflights13`)
```{r,results='hide', warning=F}
library(nycflights13)
```
Check out the `flights` object
```{r}
head(flights)
```
### Object _Structure_
Check out data _structure_ with `glimpse()`
```{r}
glimpse(flights)
```
# `dplyr` "verbs"
* `select()` and `rename()`: Extract existing variables
* `filter()` and `slice()`: Extract existing observations
* `arrange()`
* `distinct()`
* `mutate()` and `transmute()`: Derive new variables
* `summarise()`: Change the unit of analysis
* `sample_n()` and `sample_frac()`
## Useful select functions
* "`-`" Select everything but
* "`:`" Select range
* `contains()` Select columns whose name contains a character string
* `ends_with()` Select columns whose name ends with a string
* `everything()` Select every column
* `matches()` Select columns whose name matches a regular expression
* `num_range()` Select columns named x1, x2, x3, x4, x5
* `one_of()` Select columns whose names are in a group of names
* `starts_with()` Select columns whose name starts with a character string
### `select()` examples
Select only the `year`, `month`, and `day` columns:
```{r}
select(flights,year, month, day)
```
### `select()` examples
Select everything _except_ the `tailnum`:
```{r}
select(flights,-tailnum)
```
Select all columns containing the string `"time"`:
```{r}
select(flights,contains("time"))
```
You can also rename columns with `select()`
```{r}
select(flights,year,carrier,destination=dest)
```
## `filter()` observations
Filter all flights that departed on on January 1st:
```{r}
filter(flights, month == 1, day == 1)
```
## _Base_ R method
This is equivalent to the more verbose code in base R:
```{r}
flights[flights$month == 1 & flights$day == 1, ]
```
Compare with `dplyr` method:
```{r,eval=F}
filter(flights, month == 1, day == 1)
```
<div class="well">
Filter the `flights` data set to keep only evening flights (`dep_time` after 1600) in June.
<button data-toggle="collapse" class="btn btn-primary btn-sm round" data-target="#demo1">Show Solution</button>
<div id="demo1" class="collapse">
```{r, purl=F}
filter(flights,dep_time>1600,month==6)
```
</div>
</div>
## Other _boolean_ expressions
`filter()` is similar to `subset()` except it handles any number of filtering conditions joined together with `&`.
You can also use other boolean operators, such as _OR_ ("|"):
```{r}
filter(flights, month == 1 | month == 2)
```
<div class="well">
Filter the `flights` data set to keep only flights where the `distance` is greater than 1000 OR the `air_time` is more than 100
<button data-toggle="collapse" class="btn btn-primary btn-sm round" data-target="#demo2">Show Solution</button>
<div id="demo2" class="collapse"> <br>
```{r, purl=FALSE}
filter(flights,distance>1000|air_time>100)
```
</div>
</div>
## Select rows with `slice()`:
```{r}
slice(flights, 1:10)
```
## `arrange()` rows
`arrange()` is similar to `filter()` except it reorders instead of filtering.
```{r}
arrange(flights, year, month, day)
```
_Base_ R method:
```{r,eval=F}
flights[order(flights$year, flights$month, flights$day), ]
```
## Descending order: `desc()`
```{r}
arrange(flights, desc(arr_delay))
```
_Base_ R method:
```{r,eval=F}
flights[order(desc(flights$arr_delay)), ]
```
## Distinct: Find distinct rows
```{r}
distinct(
select(flights,carrier)
)
```
## Mutate: Derive new variables
Adds columns with calculations based on other columns.
Average air speed (miles/hour):
```{r}
mutate(flights,ave_speed=distance/(air_time/60))%>%
select(distance, air_time,ave_speed)
```
## Chaining Operations
Learn to performing multiple operations sequentially with a _pipe_ character (`%>%`)
1. Group by a variable
2. Select some columns
3. Summarize observations
4. Filter by results
With temporary objects:
```{r}
a1 <- group_by(flights, year, month, day)
a2 <- select(a1, arr_delay, dep_delay)
a3 <- summarise(a2,
arr = mean(arr_delay, na.rm = TRUE),
dep = mean(dep_delay, na.rm = TRUE))
a4 <- filter(a3, arr > 30 | dep > 30)
head(a4)
```
If you don’t want to save the intermediate results: wrap the function calls inside each other:
```{r}
filter(
summarise(
select(
group_by(flights, year, month, day),
arr_delay, dep_delay
),
arr = mean(arr_delay, na.rm = TRUE),
dep = mean(dep_delay, na.rm = TRUE)
),
arr > 30 | dep > 30
)
```
Arguments are distant from function -> difficult to read!
## Chaining Operations
`%>%` (from the dplyr package) allows you to _pipe_ together various commands.
`x %>% f(y)` turns into `f(x, y)`
So you can use it to rewrite multiple operations that you can read left-to-right, top-to-bottom:
```{r}
flights %>%
group_by(year, month, day) %>%
select(arr_delay, dep_delay) %>%
summarise(
arr = mean(arr_delay, na.rm = TRUE),
dep = mean(dep_delay, na.rm = TRUE)
) %>%
filter(arr > 30 | dep > 30)
```
## Analyze by group with `group_by()`
Perform operations by _group_: mean departure delay by airport (`origin`)
```{r}
flights %>%
group_by(origin) %>%
summarise(meanDelay = mean(dep_delay,na.rm=T))
```
Perform operations by _group_: mean and sd departure delay by airline (`carrier`)
```{r}
flights %>%
group_by(carrier) %>%
summarise(meanDelay = mean(dep_delay,na.rm=T),
sdDelay = sd(dep_delay,na.rm=T))
```
<div class="well">
Flights from which `origin` airport go the farthest (on average)? Hint: Group by airport (`origin`) then calculate the maximum flight distance (`distance`).
<button data-toggle="collapse" class="btn btn-primary btn-sm round" data-target="#demo2a">Show Solution</button>
<div id="demo2a" class="collapse">
```{r,purl=F}
flights %>%
group_by(origin) %>%
summarise(meanDist = mean(distance,na.rm=T))%>%
arrange(desc(meanDist))%>%
slice(1)%>%
select(origin)
```
</div>
</div>
# Today's task
Now [complete the task here](CS_03.html) by yourself or in small groups.
## Colophon
This exercise based on code from [here](http://spatial.ly/2012/06/mapping-worlds-biggest-airlines/).