unnest | R Documentation |
Unnest expands a list-column containing data frames into rows and columns.
unnest(
data,
cols,
...,
keep_empty = FALSE,
ptype = NULL,
names_sep = NULL,
names_repair = "check_unique",
.drop = deprecated(),
.id = deprecated(),
.sep = deprecated(),
.preserve = deprecated()
)
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 receive) 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
Other rectangling:
hoist()
,
unnest_longer()
,
unnest_wider()
# unnest() is 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, c = 4)
)
)
# unnest() recycles input rows for each row of the list-column
# and adds a column for each column
df %>% unnest(y)
# input rows with 0 rows in the list-column will usually disappear,
# but you can keep them (generating NAs) with keep_empty = TRUE:
df %>% unnest(y, keep_empty = TRUE)
# Multiple columns ----------------------------------------------------------
# You can unnest multiple columns simultaneously
df <- tibble(
x = 1:2,
y = list(
tibble(a = 1, b = 2),
tibble(a = 3:4, b = 5:6)
),
z = list(
tibble(c = 1, d = 2),
tibble(c = 3:4, d = 5:6)
)
)
df %>% unnest(c(y, z))
# Compare with unnesting one column at a time, which generates
# the Cartesian product
df %>%
unnest(y) %>%
unnest(z)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.