forked from gadenbuie/tidyexplain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.Rmd
376 lines (257 loc) · 9.7 KB
/
README.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
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r setup, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
echo = FALSE,
warning = FALSE,
message = FALSE,
cache = TRUE
)
```
[gganimate]: https://github.com/thomasp85/gganimate#README
[dplyr-two-table]: https://dplyr.tidyverse.org/articles/two-table.html
[r4ds]: http://r4ds.had.co.nz/
[r4ds-relational]: http://r4ds.had.co.nz/relational-data.html
[r4ds-set-ops]: http://r4ds.had.co.nz/relational-data.html#set-operations
[r4ds-tidy-data]: http://r4ds.had.co.nz/tidy-data.html#tidy-data-1
[tidyverse]: https://tidyverse.org
[tidyr]: https://tidyr.tidyverse.org
# Tidy Animated Verbs
Garrick Aden-Buie -- [@grrrck](https://twitter.com/grrrck) -- [garrickadenbuie.com](https://www.garrickadenbuie.com). Set operations contributed by [Tyler Grant Smith](https://github.com/TylerGrantSmith).
[![Binder](http://mybinder.org/badge.svg)](https://mybinder.org/v2/gh/gadenbuie/tidy-animated-verbs/master?urlpath=rstudio)
[![CC0](https://img.shields.io/badge/license_(images)_-CC0-green.svg)](https://creativecommons.org/publicdomain/zero/1.0/)
[![MIT](https://img.shields.io/badge/license_(code)_-MIT-green.svg)](https://opensource.org/licenses/MIT)
<!-- README START -->
- [**Mutating Joins**](#mutating-joins) — [`inner_join()`](#inner-join), [`left_join()`](#left-join),
[`right_join()`](#right-join), [`full_join()`](#full-join)
- [**Filtering Joins**](#filtering-joins) — [`semi_join()`](#semi-join), [`anti_join()`](#anti-join)
- [**Set Operations**](#set-operations) — [`union()`](#union), [`union_all()`](#union-all), [`intersect()`](#intersection), [`setdiff()`](#set-difference)
- [**Tidy Data**](#tidy-data) — [`spread()` and `gather()`](#spread-and-gather)
- Learn more about
- [Using the animations and images](#usage)
- [Relational Data](#relational-data)
- [gganimate](#gganimate)
## Background
### Usage
Please feel free to use these images for teaching or learning about action verbs from the [tidyverse](https://tidyverse.org).
You can directly download the [original animations](images/) or static images in [svg](images/static/svg/) or [png](images/static/png/) formats, or you can use the [scripts](R/) to recreate the images locally.
Currently, the animations cover the [dplyr two-table verbs][dplyr-two-table] and I'd like to expand the animations to include more verbs from the tidyverse.
[Suggestions are welcome!](https://github.com/gadenbuie/tidy-animated-verbs/issues)
### Relational Data
The [Relational Data][r4ds-relational] chapter of the
[R for Data Science][r4ds] book by Garrett Grolemund and Hadley Wickham
is an excellent resource for learning more about relational data.
The [dplyr two-table verbs vignette][dplyr-two-table]
and Jenny Bryan's [Cheatsheet for dplyr join functions](http://stat545.com/bit001_dplyr-cheatsheet.html)
are also great resources.
### gganimate
The animations were made possible by the newly re-written [gganimate] package by
[Thomas Lin Pedersen](https://github.com/thomasp85)
(original by [Dave Robinson](https://github.com/dgrtwo)).
The [package readme][gganimate] provides an excellent (and quick) introduction to gganimate.
### Dynamic Animations
Thanks to an initial push by [David Zimmermann](https://github.com/DavZim), we have begun work towards functions that generate dynamic animations from users' actual data.
Please visit the [pkg branch](https://github.com/gadenbuie/tidyexplain/tree/pkg) of the tidyexplain repository for more information (or to contribute!).
## Mutating Joins
> A mutating join allows you to combine variables from two tables. It first matches observations by their keys, then copies across variables from one table to the other.
> [R for Data Science: Mutating joins](http://r4ds.had.co.nz/relational-data.html#mutating-joins)
```{r intial-dfs}
source("R/00_base_join.R")
df_names <- tibble(
.x = c(1.5, 4.5), .y = 0.25,
value = c("x", "y"),
size = 12,
color = "black"
)
g <- plot_data(initial_join_dfs) +
geom_text(data = df_names, family = "Fira Mono", size = 24)
save_static_plot(g, "original-dfs")
```
<img src="images/static/png/original-dfs.png" width="480px" />
```{r echo=TRUE}
x
y
```
### Inner Join
> All rows from `x` where there are matching values in `y`, and all columns from `x` and `y`.
```{r inner-join}
source("R/inner_join.R")
```
![](images/inner-join.gif)
```{r echo=TRUE}
inner_join(x, y, by = "id")
```
### Left Join
> All rows from `x`, and all columns from `x` and `y`. Rows in `x` with no match in `y` will have `NA` values in the new columns.
```{r left-join}
source("R/left_join.R")
```
![](images/left-join.gif)
```{r echo=TRUE}
left_join(x, y, by = "id")
```
### Left Join (Extra Rows in y)
> ... If there are multiple matches between `x` and `y`, all combinations of the matches are returned.
```{r left-join-extra}
source("R/left_join_extra.R")
```
![](images/left-join-extra.gif)
```{r echo=TRUE}
y_extra # has multiple rows with the key from `x`
left_join(x, y_extra, by = "id")
```
### Right Join
> All rows from y, and all columns from `x` and `y`. Rows in `y` with no match in `x` will have `NA` values in the new columns.
```{r right-join}
source("R/right_join.R")
```
![](images/right-join.gif)
```{r echo=TRUE}
right_join(x, y, by = "id")
```
### Full Join
> All rows and all columns from both `x` and `y`. Where there are not matching values, returns `NA` for the one missing.
```{r full-join}
source("R/full_join.R")
```
![](images/full-join.gif)
```{r echo=TRUE}
full_join(x, y, by = "id")
```
## Filtering Joins
> Filtering joins match observations in the same way as mutating joins, but affect the observations, not the variables.
> ... Semi-joins are useful for matching filtered summary tables back to the original rows.
> ... Anti-joins are useful for diagnosing join mismatches.
> [R for Data Science: Filtering Joins](http://r4ds.had.co.nz/relational-data.html#filtering-joins)
### Semi Join
> All rows from `x` where there are matching values in `y`, keeping just columns from `x`.
```{r semi-join}
source("R/semi_join.R")
```
![](images/semi-join.gif)
```{r echo=TRUE}
semi_join(x, y, by = "id")
```
### Anti Join
> All rows from `x` where there are not matching values in `y`, keeping just columns from `x`.
```{r anti-join}
source("R/anti_join.R")
```
![](images/anti-join.gif)
```{r echo=TRUE}
anti_join(x, y, by = "id")
```
## Set Operations
> Set operations are occasionally useful when you want to break a single complex filter into simpler pieces.
> All these operations work with a complete row, comparing the values of every variable.
> These expect the x and y inputs to have the same variables, and treat the observations like sets.
> [R for Data Science: Set operations](http://r4ds.had.co.nz/relational-data.html#set-operations)
```{r intial-dfs-so}
source("R/00_base_set.R")
df_names <- tibble(
.x = c(2.5, 5.5), .y = 0.25,
value = c("x", "y"),
size = 12,
color = "black"
)
g <- plot_data_set(initial_set_dfs, "", NULL, NULL) +
geom_text(data = df_names, family = "Fira Mono", size = 24)
save_static_plot(g, "original-dfs-set-ops")
```
```{r remove-set-ops-ids}
x <- x %>% select(-id)
y <- y %>% select(-id)
```
<img src="images/static/png/original-dfs-set-ops.png" width="480px" />
```{r echo=TRUE}
x
y
```
### Union
> All unique rows from `x` and `y`.
```{r union}
source("R/union.R")
<<remove-set-ops-ids>>
```
![](images/union.gif)
```{r echo=TRUE}
union(x, y)
```
![](images/union-rev.gif)
```{r echo=TRUE}
union(y, x)
```
### Union All
> All rows from `x` and `y`, keeping duplicates.
```{r union-all}
source("R/union_all.R")
<<remove-set-ops-ids>>
```
![](images/union-all.gif)
```{r echo=TRUE}
union_all(x, y)
```
### Intersection
> Common rows in both `x` and `y`, keeping just unique rows.
```{r intersect}
source("R/intersect.R")
<<remove-set-ops-ids>>
```
![](images/intersect.gif)
```{r echo=TRUE}
intersect(x, y)
```
### Set Difference
> All rows from `x` which are not also rows in `y`, keeping just unique rows.
```{r setdiff}
source("R/setdiff.R")
<<remove-set-ops-ids>>
```
![](images/setdiff.gif)
```{r echo=TRUE}
setdiff(x, y)
```
![](images/setdiff-rev.gif)
```{r echo=TRUE}
setdiff(y, x)
```
## Tidy Data
[Tidy data][r4ds-tidy-data] follows the following three rules:
1. Each variable has its own column.
1. Each observation has its own row.
1. Each value has its own cell.
Many of the tools in the [tidyverse] expect data to be formatted as a tidy dataset and the [tidyr] package provides functions to help you organize your data into tidy data.
```{r tidyr-wide-long}
source("R/tidyr_spread_gather.R")
tidy_plots <- list()
tidy_plots$wide <- bind_rows(sg_wide, sg_wide_labels)
tidy_plots$long <- bind_rows(sg_long, sg_long_labels)
tidy_plots <- map(tidy_plots, ~ mutate(.,
.text_color = ifelse(grepl("id|key|val", value), "black", "white"),
.text_size = ifelse(grepl("id|key|val", value), 6, 10)
)) %>%
imap(~ plot_data(.x, .y))
tidy_plots$wide <- tidy_plots$wide + ylim(-6.5, 0.5)
save_static_plot(cowplot::plot_grid(plotlist = tidy_plots, axis = "t"), "original-dfs-tidy")
```
![](images/static/png/original-dfs-tidy.png)
```{r echo=TRUE}
wide
long
```
### Spread and Gather
`spread(data, key, value)`
> Spread a key-value pair across multiple columns.
> Use it when an a column contains observations from multiple variables.
`gather(data, key = "key", value = "value", ...)`
> Gather takes multiple columns and collapses into key-value pairs, duplicating all other columns as needed.
> You use `gather()` when you notice that your column names are not names of variables, but *values* of a variable.
![](images/tidyr-spread-gather.gif)
```{r echo=TRUE}
gather(wide, key, val, x:z)
spread(long, key, val)
```