Tidy Animated Verbs
December 30, 2021 · View on GitHub
Garrick Aden-Buie – @grrrck – garrickadenbuie.com
Thanks to contributions from …
- Tyler Grant Smith contributed set operations animations.
- Lukas Wallrich and Kelsey Gonzalez helped create animations of tidyr’s pivoting functions.
Animations:
-
Mutating Joins —
inner_join(),left_join(),right_join(),full_join() -
Set Operations —
union(),union_all(),intersect(),setdiff() -
Tidy Data —
pivot_wider()andpivot_longer(),spread()andgather() -
Learn more about
Background
Usage
Please feel free to use these images for teaching or learning about action verbs from the tidyverse. You can directly download the original animations or static images in svg or png formats, or you can use the scripts to recreate the images locally.
Currently, the animations cover the dplyr two-table verbs and I’d like to expand the animations to include more verbs from the tidyverse. Suggestions are welcome!
Relational Data
The Relational Data chapter of the R for Data Science book by Garrett Grolemund and Hadley Wickham is an excellent resource for learning more about relational data.
The dplyr two-table verbs vignette and Jenny Bryan’s Cheatsheet for dplyr join functions are also great resources.
gganimate
The animations were made possible by the newly re-written gganimate package by Thomas Lin Pedersen (original by Dave Robinson). The package readme provides an excellent (and quick) introduction to gganimate.
Dynamic Animations
Thanks to an initial push by David Zimmermann, we have begun work towards functions that generate dynamic animations from users’ actual data. Please visit the pkg branch 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
$ \text{r} \text{x} #> # \text{A} \text{tibble}: 3 \times 2 #> \text{id} \text{x} #> <\text{int}> <\text{chr}> #> 1 1 \text{x1} #> 2 2 \text{x2} #> 3 3 \text{x3} \text{y} #> # \text{A} \text{tibble}: 3 \times 2 #> \text{id} \text{y} #> <\text{int}> <\text{chr}> #> 1 1 \text{y1} #> 2 2 \text{y2} #> 3 4 \text{y4} $
Inner Join
All rows from
xwhere there are matching values iny, and all columns fromxandy.

$ \text{r} \text{inner\_join}(\text{x}, \text{y}, \text{by} = "\text{id}") #> # \text{A} \text{tibble}: 2 \times 3 #> \text{id} \text{x} \text{y} #> <\text{int}> <\text{chr}> <\text{chr}> #> 1 1 \text{x1} \text{y1} #> 2 2 \text{x2} \text{y2} $
Left Join
All rows from
x, and all columns fromxandy. Rows inxwith no match inywill haveNAvalues in the new columns.

$ \text{r} \text{left\_join}(\text{x}, \text{y}, \text{by} = "\text{id}") #> # \text{A} \text{tibble}: 3 \times 3 #> \text{id} \text{x} \text{y} #> <\text{int}> <\text{chr}> <\text{chr}> #> 1 1 \text{x1} \text{y1} #> 2 2 \text{x2} \text{y2} #> 3 3 \text{x3} <\text{NA}> $
Left Join (Extra Rows in y)
… If there are multiple matches between
xandy, all combinations of the matches are returned.

y_extra # has multiple rows with the key from `x$
#> # \text{A} \text{tibble}: 4 \times 2
#> \text{id} \text{y}
#> <\text{dbl}> <\text{chr}>
#> 1 1 \text{y1}
#> 2 2 \text{y2}
#> 3 4 \text{y4}
#> 4 2 \text{y5}
\text{left\_join}(\text{x}, \text{y\_extra}, \text{by} = "\text{id}")
#> # \text{A} \text{tibble}: 4 \times 3
#> \text{id} \text{x} \text{y}
#> <\text{dbl}> <\text{chr}> <\text{chr}>
#> 1 1 \text{x1} \text{y1}
#> 2 2 \text{x2} \text{y2}
#> 3 2 \text{x2} \text{y5}
#> 4 3 \text{x3} <\text{NA}>
$``
### 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.

``$ \text{r}
\text{right\_join}(\text{x}, \text{y}, \text{by} = "\text{id}")
#> # \text{A} \text{tibble}: 3 \times 3
#> \text{id} \text{x} \text{y}
#> <\text{int}> <\text{chr}> <\text{chr}>
#> 1 1 \text{x1} \text{y1}
#> 2 2 \text{x2} \text{y2}
#> 3 4 <\text{NA}> \text{y4}
$``
### Full Join
> All rows and all columns from both `x` and `y`. Where there are not
> matching values, returns `NA` for the one missing.

``$ \text{r}
\text{full\_join}(\text{x}, \text{y}, \text{by} = "\text{id}")
#> # \text{A} \text{tibble}: 4 \times 3
#> \text{id} \text{x} \text{y}
#> <\text{int}> <\text{chr}> <\text{chr}>
#> 1 1 \text{x1} \text{y1}
#> 2 2 \text{x2} \text{y2}
#> 3 3 \text{x3} <\text{NA}>
#> 4 4 <\text{NA}> \text{y4}
$``
## 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`.

``$ \text{r}
\text{semi\_join}(\text{x}, \text{y}, \text{by} = "\text{id}")
#> # \text{A} \text{tibble}: 2 \times 2
#> \text{id} \text{x}
#> <\text{int}> <\text{chr}>
#> 1 1 \text{x1}
#> 2 2 \text{x2}
$``
### Anti Join
> All rows from `x` where there are not matching values in `y`, keeping
> just columns from `x`.

``$ \text{r}
\text{anti\_join}(\text{x}, \text{y}, \text{by} = "\text{id}")
#> # \text{A} \text{tibble}: 1 \times 2
#> \text{id} \text{x}
#> <\text{int}> <\text{chr}>
#> 1 3 \text{x3}
$``
## 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)
<img src="images/static/png/original-dfs-set-ops.png" width="480px" />
``$ \text{r}
\text{x}
#> # \text{A} \text{tibble}: 3 \times 2
#> \text{x} \text{y}
#> <\text{chr}> <\text{chr}>
#> 1 1 \text{a}
#> 2 1 \text{b}
#> 3 2 \text{a}
\text{y}
#> # \text{A} \text{tibble}: 2 \times 2
#> \text{x} \text{y}
#> <\text{chr}> <\text{chr}>
#> 1 1 \text{a}
#> 2 2 \text{b}
$``
### Union
> All unique rows from `x` and `y`.

``$ \text{r}
\text{union}(\text{x}, \text{y})
#> # \text{A} \text{tibble}: 4 \times 2
#> \text{x} \text{y}
#> <\text{chr}> <\text{chr}>
#> 1 1 \text{a}
#> 2 1 \text{b}
#> 3 2 \text{a}
#> 4 2 \text{b}
$``

``$ \text{r}
\text{union}(\text{y}, \text{x})
#> # \text{A} \text{tibble}: 4 \times 2
#> \text{x} \text{y}
#> <\text{chr}> <\text{chr}>
#> 1 1 \text{a}
#> 2 2 \text{b}
#> 3 1 \text{b}
#> 4 2 \text{a}
$``
### Union All
> All rows from `x` and `y`, keeping duplicates.

``$ \text{r}
\text{union\_all}(\text{x}, \text{y})
#> # \text{A} \text{tibble}: 5 \times 2
#> \text{x} \text{y}
#> <\text{chr}> <\text{chr}>
#> 1 1 \text{a}
#> 2 1 \text{b}
#> 3 2 \text{a}
#> 4 1 \text{a}
#> 5 2 \text{b}
$``
### Intersection
> Common rows in both `x` and `y`, keeping just unique rows.

``$ \text{r}
\text{intersect}(\text{x}, \text{y})
#> # \text{A} \text{tibble}: 1 \times 2
#> \text{x} \text{y}
#> <\text{chr}> <\text{chr}>
#> 1 1 \text{a}
$``
### Set Difference
> All rows from `x` which are not also rows in `y`, keeping just unique
> rows.

``$ \text{r}
\text{setdiff}(\text{x}, \text{y})
#> # \text{A} \text{tibble}: 2 \times 2
#> \text{x} \text{y}
#> <\text{chr}> <\text{chr}>
#> 1 1 \text{b}
#> 2 2 \text{a}
$``

``$ \text{r}
\text{setdiff}(\text{y}, \text{x})
#> # \text{A} \text{tibble}: 1 \times 2
#> \text{x} \text{y}
#> <\text{chr}> <\text{chr}>
#> 1 2 \text{b}
$``
## Tidy Data
[Tidy data](http://r4ds.had.co.nz/tidy-data.html#tidy-data-1) follows
the following three rules:
1. Each variable has its own column.
2. Each observation has its own row.
3. Each value has its own cell.
Many of the tools in the [tidyverse](https://tidyverse.org) expect data
to be formatted as a tidy dataset and the
[tidyr](https://tidyr.tidyverse.org) package provides functions to help
you organize your data into tidy data.

``$ \text{r}
\text{wide}
#> # \text{A} \text{tibble}: 2 \times 4
#> \text{id} \text{x} \text{y} \text{z}
#> <\text{int}> <\text{chr}> <\text{chr}> <\text{chr}>
#> 1 1 \text{a} \text{c} \text{e}
#> 2 2 \text{b} \text{d} \text{f}
\text{long}
#> # \text{A} \text{tibble}: 6 \times 3
#> \text{id} \text{key} \text{val}
#> <\text{int}> <\text{chr}> <\text{chr}>
#> 1 1 \text{x} \text{a}
#> 2 2 \text{x} \text{b}
#> 3 1 \text{y} \text{c}
#> 4 2 \text{y} \text{d}
#> 5 1 \text{z} \text{e}
#> 6 2 \text{z} \text{f}
$``
### Pivot Wider and Longer
`pivot_wider()` and `pivot_longer()` were introduced in [tidyr version
1.0](https://www.tidyverse.org/blog/2019/09/tidyr-1-0-0/#pivoting)
(released in September 2019). They provide a more consistent and more
powerful approach to changing the fundamental shape of the data and are
“modern alternatives to `spread()` and `gather()`.
Here we show the very basic mechanics of pivoting, but there’s much more
that the pivot functions can do. You can learn more about them in the
[Pivoting vignette in
tidyr](https://tidyr.tidyverse.org/articles/pivot.html).
``` r
pivot_wider(data, names_from = key, values_from = val)
pivot_wider()“widens” data, increasing the number of columns and decreasing the number of rows.
pivot_longer(data, cols = x:y, names_to = "key", values_to = "val")
pivot_longer()“lengthens” data, increasing the number of rows and decreasing the number of columns.

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.

$ \text{r} \text{gather}(\text{wide}, \text{key}, \text{val}, \text{x}:\text{z}) #> # \text{A} \text{tibble}: 6 \times 3 #> \text{id} \text{key} \text{val} #> <\text{int}> <\text{chr}> <\text{chr}> #> 1 1 \text{x} \text{a} #> 2 2 \text{x} \text{b} #> 3 1 \text{y} \text{c} #> 4 2 \text{y} \text{d} #> 5 1 \text{z} \text{e} #> 6 2 \text{z} \text{f} \text{spread}(\text{long}, \text{key}, \text{val}) #> # \text{A} \text{tibble}: 2 \times 4 #> \text{id} \text{x} \text{y} \text{z} #> <\text{int}> <\text{chr}> <\text{chr}> <\text{chr}> #> 1 1 \text{a} \text{c} \text{e} #> 2 2 \text{b} \text{d} \text{f} $