Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
David 779aef6183 working gather and spread pirms 7 gadiem
R working gather and spread pirms 7 gadiem
README_files/figure-gfm updated readme to show the graphics pirms 7 gadiem
images updated readme to show the graphics pirms 7 gadiem
man working gather and spread pirms 7 gadiem
.Rbuildignore added package structure pirms 7 gadiem
.gitignore Fix #4 semi_join second key frame pirms 7 gadiem
DESCRIPTION working gather and spread pirms 7 gadiem
LICENSE License: CC0 public domain pirms 7 gadiem
NAMESPACE working gather and spread pirms 7 gadiem
README.Rmd working gather and spread pirms 7 gadiem
README.md working gather and spread pirms 7 gadiem
install.R Initial commit pirms 7 gadiem
runtime.txt Add TOC and binder badge pirms 7 gadiem
tidy-animated-verbs.Rproj Initial commit pirms 7 gadiem

README.md

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!

Installing

The library can be installed with

# install.package("devtools")
devtools::install_github("gadenbuie/tidy-animated-verbs")

Mutating Joins

library(tidyAnimatedVerbs)
x <- data_frame(
  id = 1:3,
  x = paste0("x", 1:3)
)

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

Inner Join

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

animate_inner_join(x, y, by = "id")

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.

animate_left_join(x, y, by = "id")

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 <- bind_rows(y, 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")

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.

animate_right_join(x, y, by = "id")

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.

animate_full_join(x, y, by = "id")

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.

animate_semi_join(x, y, by = "id")

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.

animate_anti_join(x, y, by = "id")

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

Set Operations


x <- tibble::tribble(
  ~x,    ~y,
  "1",  "a",
  "1",  "b",
  "2",  "a"
)

y <- tibble::tribble(
  ~x, ~y,
  "1",  "a",
  "2",  "b"
)

animate_union(x, y, export = "first")

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.

animate_union(x, 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
animate_union(y, x)


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.

animate_union_all(x, y)

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.

animate_intersect(x, y)

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.

animate_setdiff(x, y)

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


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

Tidy Data and gather(), spread() functionality

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

animate_gather(wide, key = "person", value = "sales", -year)

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    95

Spread

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")

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      95

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.