**R Tutorials – Omni Analytics Group**, and kindly contributed to R-bloggers)

In our previous series on Animating Data Transformations, we showed you how to use *gganimate* to construct an animation which illustrates the process of going between tall and wide representations of data. Today, we will show the same procedure for constructing an animation of the *unnest()* function.

The *unnest()* function takes a tibble containing a list column and converts it to a tibble such that each element in the list comprises a single row. Think of it as “unpacking” the list column into a more standard vector column in R.

We will create a toy dataset using the sleep data, and ensure that it contains a list column:

library(tidyverse) library(gganimate) sleep_data % mutate(group = rep(1:4, each = 5), ID = rep(1:4, each = 5)) %>% select(id = ID, group, data = extra) sleep_data

```
# A tibble: 20 x 3
id group data
```
1 1 1 0.7
2 1 1 -1.6
3 1 1 -0.2
4 1 1 -1.2
5 1 1 -0.1
...

You can see that we have a 20 row by 3 column tibble in R. Next, we perform the following routine in order to nest the data column into a new *nesteddata* column:

sleep_nested % group_by(group) %>% summarise(id = id[1], nesteddata = list(data))

sleep_nested

```
# A tibble: 4 x 3
group id nesteddata
```
1 1 1
2 2 2
3 3 3
4 4 4

Next, we perform a similar routine to the previous blog and combine the two datasets into one dataset which will be used to build the animation:

longDat % rbind(x) %>% setNames(seq_len(ncol(x))) %>% mutate(row = row_number()) %>% tidyr::gather(column, value, -row) %>% mutate(column = as.integer(column)) %>% ungroup() %>% arrange(column, row) } long_tables % mutate(tstep = "a", value = sapply(value, paste, collapse = ", ")) unnested_table % mutate(tstep = "b") both_tables`# A tibble: 78 x 5 row column value tstep celltype`

1 1 1 group a header 2 2 1 1 a id 3 3 1 2 a id 4 4 1 3 a id 5 5 1 4 a id 6 1 2 id a header 7 2 2 1 a 1 8 3 2 2 a 2 9 4 2 3 a 3 10 5 2 4 a 4 # … with 68 more rows From this, we can produce static versions of the two images which will form the basis for the animation:

base_plotFinally, we use gganimate to build the final animation!

p1And there you have it! We hope this was helpful both in learning how to produce data transformation animations, and in terms of learning the unnest() operation itself. If you have any requests for more data transformation animations, please let us know, and be on the look out for future posts in this series!

The post Animating Data Transformations: Part II appeared first on Omni Analytics Group.

Toleave a commentfor the author, please follow the link and comment on their blog:R Tutorials – Omni Analytics Group.

R-bloggers.com offersdaily e-mail updatesabout R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...