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
/
03b_DataWrangling.Rmd
167 lines (130 loc) · 4.52 KB
/
03b_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
---
title : "Data Wrangling 2"
---
```{r, echo=FALSE, message=FALSE, results='hide', purl=FALSE}
source("knitr_header.R")
```
[<i class="fas fa-desktop fa-3x" aria-hidden="true"></i> Presentation](presentations/day_07.html){target="_blank"}
[<i class="fa fa-file-code-o fa-3x" aria-hidden="true"></i> R Script](`r output`){target="_blank"} Download this file and open it (or copy-paste into a new script) with RStudio so you can follow along.
```{r,results='hide', message=FALSE, warning=F}
library(tidyverse)
library(nycflights13)
```
# Combining data sets
## `dplyr` _join_ methods
<img src="03_assets/join1.png" alt="Drawing" style="width: 50%;"/>
* `left_join(a, b, by = "x1")` Join matching rows from b to a.
* `right_join(a, b, by = "x1")` Join matching rows from a to b.
* `inner_join(a, b, by = "x1")` Retain only rows in both sets.
* `full_join(a, b, by = "x1")` Join data. Retain all values, all rows.
### Left Join
`left_join(a, b, by = "x1")` Join matching rows from b to a.
<img src="03_assets/join1.png" alt="Drawing" style="width: 50%;"/>
<img src="03_assets/join_left.png" alt="Drawing" style="width: 50%;"/>
### Right Join
`right_join(a, b, by = "x1")` Join matching rows from a to b.
<img src="03_assets/join1.png" alt="Drawing" style="width: 50%;"/>
<img src="03_assets/join_right.png" alt="Drawing" style="width: 50%;"/>
### Inner Join
`inner_join(a, b, by = "x1")` Retain only rows in both sets.
<img src="03_assets/join1.png" alt="Drawing" style="width: 50%;"/>
<img src="03_assets/join_inner.png" alt="Drawing" style="width: 50%;"/>
### Full Join
`full_join(a, b, by = "x1")` Join data. Retain all values, all rows.
<img src="03_assets/join1.png" alt="Drawing" style="width: 50%;"/>
<img src="03_assets/join_full.png" alt="Drawing" style="width: 50%;"/>
```{r}
flights%>%
select(-year,-month,-day,-hour,-minute,-dep_time,-dep_delay)%>%
glimpse()
```
Let's look at the `airports` data table (`?airports` for documentation):
```{r}
glimpse(airports)
```
Now [complete the task here](CS_04.html) by yourself or in small groups.
# Extras
If you made it through the material above, here's an example of some more 'advanced' coding to extract the geographic locations for all flights and plotting the connections as 'great circles' on a map. This is just meant as an example to illustrate how one might use these functions to perform a more advanced analysis and spatial visualization.
### Join destination airports
```{r, result=F, warning=F, message=F}
library(geosphere)
library(rgdal)
library(maps)
library(ggplot2)
library(sp)
library(rgeos)
```
```{r}
data=
select(airports,
dest=faa,
destName=name,
destLat=lat,
destLon=lon)%>%
right_join(flights)%>%
group_by(dest,
destLon,
destLat,
distance)%>%
summarise(count=n())%>%
ungroup()%>%
select(destLon,
destLat,
count,
distance)%>%
mutate(id=row_number())%>%
na.omit()
NYCll=airports%>%filter(faa=="JFK")%>%select(lon,lat) # get NYC coordinates
# calculate great circle routes
rts <- gcIntermediate(as.matrix(NYCll),
as.matrix(select(data,destLon,destLat)),
1000,
addStartEnd=TRUE,
sp=TRUE)
rts.ff <- fortify(
as(rts,"SpatialLinesDataFrame")) # convert into something ggplot can plot
## join with count of flights
rts.ff$id=as.integer(rts.ff$id)
gcircles <- left_join(rts.ff,
data,
by="id") # join attributes, we keep them all, just in case
```
Now build a basemap using data in the `maps` package.
```{r fig.width=10,fig.height=6,dpi=300}
base = ggplot()
worldmap <- map_data("world",
ylim = c(10, 70),
xlim = c(-160, -80))
wrld <- c(geom_polygon(
aes(long, lat, group = group),
size = 0.1,
colour = "grey",
fill = "grey",
alpha = 1,
data = worldmap
))
```
Now draw the map using `ggplot`
```{r}
base + wrld +
geom_path(
data = gcircles,
aes(
long,
lat,
col = count,
group = group,
),
alpha = 0.5,
lineend = "round",
lwd = 1
) +
coord_equal() +
scale_colour_gradientn(colours = c("blue", "orange", "red"),
guide = "colourbar") +
theme(panel.background = element_rect(fill = 'white', colour = 'white')) +
labs(y = "Latitude", x = "Longitude",
title = "Count of Flights from New York in 2013")
```
## Colophon
This exercise based on code from [here](http://spatial.ly/2012/06/mapping-worlds-biggest-airlines/).