forked from STAT545-UBC/STAT545-UBC-original-website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bit001_dplyr-cheatsheet.rmd
340 lines (271 loc) · 9.22 KB
/
bit001_dplyr-cheatsheet.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
---
title: "Cheatsheet for dplyr join functions"
author: "Jenny Bryan"
output:
html_document:
toc: true
toc_depth: 4
---
```{r setup, include = FALSE, cache = FALSE}
knitr::opts_chunk$set(error = TRUE, collapse = TRUE, comment = "#>")
```
#### Why the cheatsheet
Examples for those of us who don't speak SQL so good. There are lots of [Venn diagrams re: SQL joins on the interwebs](//encrypted.google.com/search?q=sql+join&tbm=isch), but I wanted R examples.
[Full documentation](http://www.rdocumentation.org/packages/dplyr) for the dplyr package, which is developed by Hadley Wickham and Romain Francois on [GitHub](https://github.com/hadley/dplyr). The [vignette on Two-table verbs](https://cran.r-project.org/web/packages/dplyr/vignettes/two-table.html) covers the joins shown here.
Working with two small data.frames, `superheroes` and `publishers`.
```{r}
suppressPackageStartupMessages(library(dplyr))
library(readr)
superheroes <- "
name, alignment, gender, publisher
Magneto, bad, male, Marvel
Storm, good, female, Marvel
Mystique, bad, female, Marvel
Batman, good, male, DC
Joker, bad, male, DC
Catwoman, bad, female, DC
Hellboy, good, male, Dark Horse Comics
"
superheroes <- read_csv(superheroes, trim_ws = TRUE, skip = 1)
publishers <- "
publisher, yr_founded
DC, 1934
Marvel, 1939
Image, 1992
"
publishers <- read_csv(publishers, trim_ws = TRUE, skip = 1)
```
Sorry, cheat sheet does not illustrate "multiple match" situations terribly well.
Sub-plot: watch the row and variable order of the join results for a healthy reminder of why it's dangerous to rely on any of that in an analysis.
#### inner_join(superheroes, publishers)
> inner_join(x, y): Return all rows from x where there are matching values in y, and all columns from x and y. If there are multiple matches between x and y, all combination of the matches are returned. This is a mutating join.
```{r}
(ijsp <- inner_join(superheroes, publishers))
```
We lose Hellboy in the join because, although he appears in `x = superheroes`, his publisher Dark Horse Comics does not appear in `y = publishers`. The join result has all variables from `x = superheroes` plus `yr_founded`, from `y`.
```{r include = FALSE}
superheroes_kable <- knitr::kable(superheroes)
publishers_kable <- knitr::kable(publishers)
ijsp_kable <- knitr::kable(ijsp)
```
<table border = 1>
<tr>
<td valign="top">
superheroes
`r superheroes_kable`
</td>
<td valign="top">
publishers
`r publishers_kable`
</td>
<td valign="top">
inner_join(x = superheroes, y = publishers)
`r ijsp_kable`
</td>
</tr>
</table>
#### semi_join(superheroes, publishers)
> semi_join(x, y): Return all rows from x where there are matching values in y, keeping just columns from x. A semi join differs from an inner join because an inner join will return one row of x for each matching row of y, where a semi join will never duplicate rows of x. This is a filtering join.
```{r}
(sjsp <- semi_join(superheroes, publishers))
```
We get a similar result as with `inner_join()` but the join result contains only the variables originally found in `x = superheroes`. But note the row order has changed.
```{r include = FALSE}
sjsp_kable <- knitr::kable(sjsp)
```
<table border = 1>
<tr>
<td valign="top">
superheroes
`r superheroes_kable`
</td>
<td valign="top">
publishers
`r publishers_kable`
</td>
<td valign="top">
semi-join(x = superheroes, y = publishers)
`r sjsp_kable`
</td>
</tr>
</table>
#### left_join(superheroes, publishers)
> left_join(x, y): Return all rows from x, and all columns from x and y. If there are multiple matches between x and y, all combination of the matches are returned. This is a mutating join.
```{r}
(ljsp <- left_join(superheroes, publishers))
```
We basically get `x = superheroes` back, but with the addition of variable `yr_founded`, which is unique to `y = publishers`. Hellboy, whose publisher does not appear in `y = publishers`, has an `NA` for `yr_founded`.
```{r include = FALSE}
ljsp_kable <- knitr::kable(ljsp)
```
<table border = 1>
<tr>
<td valign="top">
superheroes
`r superheroes_kable`
</td>
<td valign="top">
publishers
`r publishers_kable`
</td>
<td valign="top">
left_join(x = superheroes, y = publishers)
`r ljsp_kable`
</td>
</tr>
</table>
#### anti_join(superheroes, publishers)
> anti_join(x, y): Return all rows from x where there are not matching values in y, keeping just columns from x. This is a filtering join.
```{r}
(ajsp <- anti_join(superheroes, publishers))
```
We keep __only__ Hellboy now (and do not get `yr_founded`).
```{r include = FALSE}
ajsp_kable <- knitr::kable(ajsp)
```
<table border = 1>
<tr>
<td valign="top">
superheroes
`r superheroes_kable`
</td>
<td valign="top">
publishers
`r publishers_kable`
</td>
<td valign="top">
anti_join(x = superheroes, y = publishers)
`r ajsp_kable`
</td>
</tr>
</table>
#### inner_join(publishers, superheroes)
> inner_join(x, y): Return all rows from x where there are matching values in y, and all columns from x and y. If there are multiple matches between x and y, all combination of the matches are returned. This is a mutating join.
```{r}
(ijps <- inner_join(publishers, superheroes))
```
In a way, this does illustrate multiple matches, if you think about it from the `x = publishers` direction. Every publisher that has a match in `y = superheroes` appears multiple times in the result, once for each match. In fact, we're getting the same result as with `inner_join(superheroes, publishers)`, up to variable order (which you should also never rely on in an analysis).
```{r include = FALSE}
ijps_kable <- knitr::kable(ijps)
```
<table border = 1>
<tr>
<td valign="top">
publishers
`r publishers_kable`
</td>
<td valign="top">
superheroes
`r superheroes_kable`
</td>
<td valign="top">
inner_join(x = publishers, y = superheroes)
`r ijps_kable`
</td>
</tr>
</table>
#### semi_join(publishers, superheroes)
> semi_join(x, y): Return all rows from x where there are matching values in y, keeping just columns from x. A semi join differs from an inner join because an inner join will return one row of x for each matching row of y, where a semi join will never duplicate rows of x. This is a filtering join.
```{r}
(sjps <- semi_join(x = publishers, y = superheroes))
```
Now the effects of switching the `x` and `y` roles is more clear. The result resembles `x = publishers`, but the publisher Image is lost, because there are no observations where `publisher == "Image"` in `y = superheroes`.
```{r include = FALSE}
sjps_kable <- knitr::kable(sjps)
```
<table border = 1>
<tr>
<td valign="top">
publishers
`r publishers_kable`
</td>
<td valign="top">
superheroes
`r superheroes_kable`
</td>
<td valign="top">
semi-join(x = publishers, y = superheroes)
`r sjps_kable`
</td>
</tr>
</table>
#### left_join(publishers, superheroes)
> left_join(x, y): Return all rows from x, and all columns from x and y. If there are multiple matches between x and y, all combination of the matches are returned. This is a mutating join.
```{r}
(ljps <- left_join(publishers, superheroes))
```
We get a similar result as with `inner_join()` but the publisher Image survives in the join, even though no superheroes from Image appear in `y = superheroes`. As a result, Image has `NA`s for `name`, `alignment`, and `gender`.
```{r include = FALSE}
ljps_kable <- knitr::kable(ljps)
```
<table border = 1>
<tr>
<td valign="top">
publishers
`r publishers_kable`
</td>
<td valign="top">
superheroes
`r superheroes_kable`
</td>
<td valign="top">
left_join(x = publishers, y = superheroes)
`r ljps_kable`
</td>
</tr>
</table>
#### anti_join(publishers, superheroes)
> anti_join(x, y): Return all rows from x where there are not matching values in y, keeping just columns from x. This is a filtering join.
```{r}
(ajps <- anti_join(publishers, superheroes))
```
We keep __only__ publisher Image now (and the variables found in `x = publishers`).
```{r include = FALSE}
ajps_kable <- knitr::kable(ajps)
```
<table border = 1>
<tr>
<tr>
<td valign="top">
publishers
`r publishers_kable`
</td>
<td valign="top">
superheroes
`r superheroes_kable`
</td>
<td valign="top">
anti_join(x = publishers, y = superheroes)
`r ajps_kable`
</td>
</tr>
</table>
#### full_join(superheroes, publishers)
> full_join(x, y): Return all rows and all columns from both x and y. Where there are not matching values, returns NA for the one missing. This is a mutating join.
```{r}
(fjsp <- full_join(superheroes, publishers))
```
We get all rows of `x = superheroes` plus a new row from `y = publishers`, containing the publisher Image. We get all variables from `x = superheroes` AND all variables from `y = publishers`. Any row that derives solely from one table or the other carries `NA`s in the variables found only in the other table.
```{r include = FALSE}
fjsp_kable <- knitr::kable(fjsp)
```
<table border = 1>
<tr>
<td valign="top">
superheroes
`r superheroes_kable`
</td>
<td valign="top">
publishers
`r publishers_kable`
</td>
<td valign="top">
full_join(x = superheroes, y = publishers)
`r fjsp_kable`
</td>
</tr>
</table>
#### sessionInfo()
```{r}
devtools::session_info()
```