Tidy Animated Verbs
Garrick Aden-Buie ā @grrrck ā garrickadenbuie.com.
David Zimmermann ā @dav_zim ā datashenanigan.wordpress.com
Set operations contributed by Tyler Grant Smith.
-
Mutating Joins:
inner_join(),left_join(),right_join(),full_join() -
Filtering Joins:
semi_join(),anti_join() -
Set Operations:
union(),union_all(),intersect(),setdiff() -
Learn more about
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!
Installing
The in-development version of tidyexplain can be installed with
devtools:
# install.package("devtools")
devtools::install_github("gadenbuie/tidy-animated-verbs")
library(tidyexplain)Mutating Joins
x <- dplyr::data_frame(
id = 1:3,
x = paste0("x", 1:3)
)
y <- dplyr::data_frame(
id = (1:4)[-3],
y = paste0("y", (1:4)[-3])
)
animate_full_join(x, y, by = c("id"), export = "first")x
#> # A tibble: 3 x 2
#> id x
#> <int> <chr>
#> 1 1 x1
#> 2 2 x2
#> 3 3 x3
y
#> # A tibble: 3 x 2
#> id y
#> <int> <chr>
#> 1 1 y1
#> 2 2 y2
#> 3 4 y4Inner Join
All rows from
xwhere there are matching values iny, and all columns fromxandy.
animate_inner_join(x, y, by = "id")dplyr::inner_join(x, y, by = "id")
#> # A tibble: 2 x 3
#> id x y
#> <int> <chr> <chr>
#> 1 1 x1 y1
#> 2 2 x2 y2Left Join
All rows from
x, and all columns fromxandy. Rows inxwith no match inywill haveNAvalues in the new columns.
animate_left_join(x, y, by = "id")dplyr::left_join(x, y, by = "id")
#> # A tibble: 3 x 3
#> id x y
#> <int> <chr> <chr>
#> 1 1 x1 y1
#> 2 2 x2 y2
#> 3 3 x3 <NA>Left Join (Extra Rows in y)
⦠If there are multiple matches between
xandy, all combinations of the matches are returned.
y_extra <- dplyr::bind_rows(y, dplyr::data_frame(id = 2, y = "y5"))
y_extra # has multiple rows with the key from `x`
#> # A tibble: 4 x 2
#> id y
#> <dbl> <chr>
#> 1 1 y1
#> 2 2 y2
#> 3 4 y4
#> 4 2 y5
animate_left_join(x, y_extra, by = "id", title_size = 22)dplyr::left_join(x, y_extra, by = "id")
#> # A tibble: 4 x 3
#> id x y
#> <dbl> <chr> <chr>
#> 1 1 x1 y1
#> 2 2 x2 y2
#> 3 2 x2 y5
#> 4 3 x3 <NA>Right Join
All rows from y, and all columns from
xandy. Rows inywith no match inxwill haveNAvalues in the new columns.
animate_right_join(x, y, by = "id")dplyr::right_join(x, y, by = "id")
#> # A tibble: 3 x 3
#> id x y
#> <int> <chr> <chr>
#> 1 1 x1 y1
#> 2 2 x2 y2
#> 3 4 <NA> y4Full Join
All rows and all columns from both
xandy. Where there are not matching values, returnsNAfor the one missing.
animate_full_join(x, y, by = "id")dplyr::full_join(x, y, by = "id")
#> # A tibble: 4 x 3
#> id x y
#> <int> <chr> <chr>
#> 1 1 x1 y1
#> 2 2 x2 y2
#> 3 3 x3 <NA>
#> 4 4 <NA> y4Filtering Joins
Semi Join
All rows from
xwhere there are matching values iny, keeping just columns fromx.
animate_semi_join(x, y, by = "id")dplyr::semi_join(x, y, by = "id")
#> # A tibble: 2 x 2
#> id x
#> <int> <chr>
#> 1 1 x1
#> 2 2 x2Anti Join
All rows from
xwhere there are not matching values iny, keeping just columns fromx.
animate_anti_join(x, y, by = "id")dplyr::anti_join(x, y, by = "id")
#> # A tibble: 1 x 2
#> id x
#> <int> <chr>
#> 1 3 x3Set Operations
x <- dplyr::data_frame(
x = c(1, 1, 2),
y = c("a", "b", "a")
)
y <- dplyr::data_frame(
x = c(1, 2),
y = c("a", "b")
)
animate_union(x, y, export = "first")x
#> # A tibble: 3 x 2
#> x y
#> <dbl> <chr>
#> 1 1 a
#> 2 1 b
#> 3 2 a
y
#> # A tibble: 2 x 2
#> x y
#> <dbl> <chr>
#> 1 1 a
#> 2 2 bUnion
All unique rows from
xandy.
animate_union(x, y)dplyr::union(x, y)
#> # A tibble: 4 x 2
#> x y
#> <dbl> <chr>
#> 1 2 b
#> 2 2 a
#> 3 1 b
#> 4 1 aanimate_union(y, x)dplyr::union(y, x)
#> # A tibble: 4 x 2
#> x y
#> <dbl> <chr>
#> 1 2 a
#> 2 1 b
#> 3 2 b
#> 4 1 aUnion All
All rows from
xandy, keeping duplicates.
animate_union_all(x, y)dplyr::union_all(x, y)
#> # A tibble: 5 x 2
#> x y
#> <dbl> <chr>
#> 1 1 a
#> 2 1 b
#> 3 2 a
#> 4 1 a
#> 5 2 bIntersection
Common rows in both
xandy, keeping just unique rows.
animate_intersect(x, y)dplyr::intersect(x, y)
#> # A tibble: 1 x 2
#> x y
#> <dbl> <chr>
#> 1 1 aSet Difference
All rows from
xwhich are not also rows iny, keeping just unique rows.
animate_setdiff(x, y)dplyr::setdiff(x, y)
#> # A tibble: 2 x 2
#> x y
#> <dbl> <chr>
#> 1 1 b
#> 2 2 aanimate_setdiff(y, x)dplyr::setdiff(y, x)
#> # A tibble: 1 x 2
#> x y
#> <dbl> <chr>
#> 1 2 bTidy Data and gather(), spread() functionality
Tidy data follows the following three rules:
- Each variable has its own column.
- Each observation has its own row.
- 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.
long <- dplyr::data_frame(
year = c(2010, 2011, 2010, 2011, 2010, 2011),
person = c("Alice", "Alice", "Bob", "Bob", "Charlie", "Charlie"),
sales = c(105, 110, 100, 97, 90, 95)
)
wide <- dplyr::data_frame(
year = 2010:2011,
Alice = c(105, 110),
Bob = c(100, 97),
Charlie = c(90, 95)
)Gather
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.
set_font_size(4, 15)
set_anim_options(anim_options(cell_width = 2))
animate_gather(wide, key = "person", value = "sales", -year)tidyr::gather(wide, key = "person", value = "sales", -year)
#> # A tibble: 6 x 3
#> year person sales
#> <int> <chr> <dbl>
#> 1 2010 Alice 105
#> 2 2011 Alice 110
#> 3 2010 Bob 100
#> 4 2011 Bob 97
#> 5 2010 Charlie 90
#> 6 2011 Charlie 95Spread
Spread a key-value pair across multiple columns. Use it when an a column contains observations from multiple variables.
animate_spread(long, key = "person", value = "sales")tidyr::spread(long, key = "person", value = "sales")
#> # A tibble: 2 x 4
#> year Alice Bob Charlie
#> <dbl> <dbl> <dbl> <dbl>
#> 1 2010 105 100 90
#> 2 2011 110 97 95Learn More
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 gganimte.
















