View source: R/unnest-longer.R
unnest_longer | R Documentation |
unnest_longer()
turns each element of a list-column into a row. It
is most naturally suited to list-columns where the elements are unnamed
and the length of each element varies from row to row.
unnest_longer()
generally preserves the number of columns of x
while
modifying the number of rows.
Learn more in vignette("rectangle")
.
unnest_longer(
data,
col,
values_to = NULL,
indices_to = NULL,
indices_include = NULL,
keep_empty = FALSE,
names_repair = "check_unique",
simplify = TRUE,
ptype = NULL,
transform = NULL
)
data |
A data frame. |
col |
< When selecting multiple columns, values from the same row will be recycled to their common size. |
values_to |
A string giving the column name (or names) to store the
unnested values in. If multiple columns are specified in |
indices_to |
A string giving the column name (or names) to store the
inner names or positions (if not named) of the values. If multiple columns
are specified in |
indices_include |
A single logical value specifying whether or not to
add an index column. If any value has inner names, the index column will be
a character vector of those names, otherwise it will be an integer vector
of positions. If If |
keep_empty |
By default, you get one row of output for each element
of the list that you are unchopping/unnesting. This means that if there's a
size-0 element (like |
names_repair |
Used to check that output data frame has valid names. Must be one of the following options:
See |
simplify |
If |
ptype |
Optionally, a named list of prototypes declaring the desired output type of each component. Alternatively, a single empty prototype can be supplied, which will be applied to all components. Use this argument if you want to check that each element has the type you expect when simplifying. If a |
transform |
Optionally, a named list of transformation functions applied to each component. Alternatively, a single function can be supplied, which will be applied to all components. Use this argument if you want to transform or parse individual elements as they are extracted. When both |
Other rectangling:
hoist()
,
unnest_wider()
,
unnest()
# `unnest_longer()` is useful when each component of the list should
# form a row
df <- tibble(
x = 1:4,
y = list(NULL, 1:3, 4:5, integer())
)
df %>% unnest_longer(y)
# Note that empty values like `NULL` and `integer()` are dropped by
# default. If you'd like to keep them, set `keep_empty = TRUE`.
df %>% unnest_longer(y, keep_empty = TRUE)
# If the inner vectors are named, the names are copied to an `_id` column
df <- tibble(
x = 1:2,
y = list(c(a = 1, b = 2), c(a = 10, b = 11, c = 12))
)
df %>% unnest_longer(y)
# Multiple columns ----------------------------------------------------------
# If columns are aligned, you can unnest simultaneously
df <- tibble(
x = 1:2,
y = list(1:2, 3:4),
z = list(5:6, 7:8)
)
df %>%
unnest_longer(c(y, z))
# This is important because sequential unnesting would generate the
# Cartesian product of the rows
df %>%
unnest_longer(y) %>%
unnest_longer(z)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.