Skip to content

DavZim/tidy-animated-verbs

This branch is 18 commits behind gadenbuie/tidyexplain:main.

Repository files navigation

Tidy Animated Verbs

Garrick Aden-Buie – @grrrckgarrickadenbuie.com. Set operations contributed by Tyler Grant Smith.

Binder CC0 MIT

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!

Mutating Joins

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 y4

Inner Join

All rows from x where there are matching values in y, and all columns from x and y.

inner_join(x, y, by = "id")
#> # A tibble: 2 x 3
#>      id x     y    
#>   <int> <chr> <chr>
#> 1     1 x1    y1   
#> 2     2 x2    y2

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.

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 x and y, all combinations of the matches are returned.

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
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 x and y. Rows in y with no match in x will have NA values in the new columns.

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>  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.

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>  y4

Filtering Joins

Semi Join

All rows from x where there are matching values in y, keeping just columns from x.

semi_join(x, y, by = "id")
#> # A tibble: 2 x 2
#>      id x    
#>   <int> <chr>
#> 1     1 x1   
#> 2     2 x2

Anti Join

All rows from x where there are not matching values in y, keeping just columns from x.

anti_join(x, y, by = "id")
#> # A tibble: 1 x 2
#>      id x    
#>   <int> <chr>
#> 1     3 x3

Set Operations

x
#> # A tibble: 3 x 2
#>   x     y    
#>   <chr> <chr>
#> 1 1     a    
#> 2 1     b    
#> 3 2     a
y 
#> # A tibble: 2 x 2
#>   x     y    
#>   <chr> <chr>
#> 1 1     a    
#> 2 2     b

Union

All unique rows from x and y.

union(x, y)
#> # A tibble: 4 x 2
#>   x     y    
#>   <chr> <chr>
#> 1 2     b    
#> 2 2     a    
#> 3 1     b    
#> 4 1     a

union(y, x)
#> # A tibble: 4 x 2
#>   x     y    
#>   <chr> <chr>
#> 1 2     a    
#> 2 1     b    
#> 3 2     b    
#> 4 1     a

Union All

All rows from x and y, keeping duplicates.

union_all(x, y)
#> # A tibble: 5 x 2
#>   x     y    
#>   <chr> <chr>
#> 1 1     a    
#> 2 1     b    
#> 3 2     a    
#> 4 1     a    
#> 5 2     b

Intersection

Common rows in both x and y, keeping just unique rows.

intersect(x, y)
#> # A tibble: 1 x 2
#>   x     y    
#>   <chr> <chr>
#> 1 1     a

Set Difference

All rows from x which are not also rows in y, keeping just unique rows.

setdiff(x, y)
#> # A tibble: 2 x 2
#>   x     y    
#>   <chr> <chr>
#> 1 1     b    
#> 2 2     a

setdiff(y, x)
#> # A tibble: 1 x 2
#>   x     y    
#>   <chr> <chr>
#> 1 2     b

Learn 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.

About

Animations of tidyverse verbs using R, the tidyverse, and gganimate

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • R 100.0%