What is the difference between (un)packing and (un)nesting a tibble?
nest
unnest
pack
unpack
tidyr
json
constructive
Author
Affiliations
Layal Christine Lettry
cynkra GmbH
University of Fribourg, Dept. of Informatics, ASAM Group
Published
May 30, 2024
Does a nested tibble have the same structure as a packed tibble?
Initial object
Let’s assume that we have the object my_tib which is a nested tibble containing a list, namely my_values, with another tibble where the variables are my_ints and my_chars.
As you can see, there is no difference between my_tib and my_nested_tib.
waldo::compare(my_tib, my_nested_tib)
✔ No differences
What is the difference between a nested and a packed tibble?
To obtain a packed tibble, we should pack the variables my_ints and my_chars together so that we have a tibble in another tibble instead of a list with an element that is a tibble.
We can assess the difference between my_nested_tib and my_packed_tib with waldo::compare().
waldo::compare(my_nested_tib, my_packed_tib)
`attr(old, 'row.names')`: 1
`attr(new, 'row.names')`: 1 2 3 4 5
`old$my_values` is a list
`new$my_values` is an S3 object of class <tbl_df/tbl/data.frame>, a list
This tells us that my_nested_tib has only one row and contains the variable my_values that is a list, whereas my_packed_tib has 5 rows and is constituted by the variable my_values that has, in this case, the class data.frame.
class(my_packed_tib$my_values)
[1] "tbl_df" "tbl" "data.frame"
For the record, a data frame is a special list where every element has the same length.
typeof(my_packed_tib$my_values)
[1] "list"
How to unnest or unpack a tibble?
To get a tibble without any variable that is a list or a tibble, we should unnest and, respectively, unpack our nested/packed tibble.
Here again, we obtain a simple tibble with two variables instead of one single variable that has the class data.frame.
What do the packed tibble and nested tibble look like in a JSON format?
The main difference is that the instances of the variable my_values of the nested tibble will be written between extra square brackets to represent the list class of my_values. On the contrary, each row of the variable my_values of the packed tibble will be displayed separately between curly brackets given that my_values has the class data.frame in the packed case.
@online{lettry2024,
author = {Lettry, Layal Christine},
title = {What Is the Difference Between (Un)packing and (Un)nesting a
Tibble?},
date = {2024-05-30},
url = {https://rdiscovery.netlify.app/posts/2024-05-30_pack-nest/},
langid = {en}
}