Nest and unnest
Nesting creates a list-column of data frames; unnesting flattens it back out into regular columns. Nesting is implicitly a summarising operation: you get one row for each group defined by the non-nested columns. This is useful in conjunction with other summaries that work with whole datasets, most notably models.
Learn more in vignette("nest")
.
nest(.data, ..., .names_sep = NULL, .key = deprecated()) unnest( data, cols, ..., keep_empty = FALSE, ptype = NULL, names_sep = NULL, names_repair = "check_unique", .drop = deprecated(), .id = deprecated(), .sep = deprecated(), .preserve = deprecated() )
.data |
A data frame. |
... |
< :
previously you could write If you previously created new variable in |
.key |
:
No longer needed because of the new |
data |
A data frame. |
cols |
< If you |
keep_empty |
By default, you get one row of output for each element
of the list your unchopping/unnesting. This means that if there's a
size-0 element (like |
ptype |
Optionally, supply a data frame prototype for the output |
names_sep, .names_sep |
If If a string, the inner and outer names will be used together. In |
names_repair |
Used to check that output data frame has valid names. Must be one of the following options:
See |
.drop, .preserve |
:
all list-columns are now preserved; If there are any that you
don't want in the output use |
.id |
:
convert |
.sep |
tidyr 1.0.0 introduced a new syntax for nest()
and unnest()
that's
designed to be more similar to other functions. Converting to the new syntax
should be straightforward (guided by the message you'll recieve) but if
you just need to run an old analysis, you can easily revert to the previous
behaviour using nest_legacy()
and unnest_legacy()
as follows:
library(tidyr) nest <- nest_legacy unnest <- unnest_legacy
df %>% nest(data = c(x, y))
specifies the columns to be nested; i.e. the
columns that will appear in the inner data frame. Alternatively, you can
nest()
a grouped data frame created by dplyr::group_by()
. The grouping
variables remain in the outer data frame and the others are nested. The
result preserves the grouping of the input.
Variables supplied to nest()
will override grouping variables so that
df %>% group_by(x, y) %>% nest(data = !z)
will be equivalent to
df %>% nest(data = !z)
.
df <- tibble(x = c(1, 1, 1, 2, 2, 3), y = 1:6, z = 6:1) # Note that we get one row of output for each unique combination of # non-nested variables df %>% nest(data = c(y, z)) # chop does something similar, but retains individual columns df %>% chop(c(y, z)) # use tidyselect syntax and helpers, just like in dplyr::select() df %>% nest(data = any_of(c("y", "z"))) iris %>% nest(data = !Species) nest_vars <- names(iris)[1:4] iris %>% nest(data = any_of(nest_vars)) iris %>% nest(petal = starts_with("Petal"), sepal = starts_with("Sepal")) iris %>% nest(width = contains("Width"), length = contains("Length")) # Nesting a grouped data frame nests all variables apart from the group vars library(dplyr) fish_encounters %>% group_by(fish) %>% nest() # Nesting is often useful for creating per group models mtcars %>% group_by(cyl) %>% nest() %>% mutate(models = lapply(data, function(df) lm(mpg ~ wt, data = df))) # unnest() is primarily designed to work with lists of data frames df <- tibble( x = 1:3, y = list( NULL, tibble(a = 1, b = 2), tibble(a = 1:3, b = 3:1) ) ) df %>% unnest(y) df %>% unnest(y, keep_empty = TRUE) # If you have lists of lists, or lists of atomic vectors, instead # see hoist(), unnest_wider(), and unnest_longer() #' # You can unnest multiple columns simultaneously df <- tibble( a = list(c("a", "b"), "c"), b = list(1:2, 3), c = c(11, 22) ) df %>% unnest(c(a, b)) # Compare with unnesting one column at a time, which generates # the Cartesian product df %>% unnest(a) %>% unnest(b)
Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.