Nest and un-nest an ir object
Description
Nest and un-nest an ir object
Usage
nest.ir(.data, ..., .names_sep = NULL, .key = deprecated())
unnest.ir(
data,
cols,
...,
keep_empty = FALSE,
ptype = NULL,
names_sep = NULL,
names_repair = "check_unique",
.drop = deprecated(),
.id = deprecated(),
.sep = deprecated(),
.preserve = deprecated()
)
Arguments
.data |
An object of class ir.
|
... |
<tidy-select> Columns to nest, specified
using name-variable pairs of the form new_col = c(col1, col2, col3).
The right hand side can be any valid tidy select expression.
:
previously you could write df %>% nest(x, y, z) and df %>% unnest(x, y, z). Convert to df %>% nest(data = c(x, y, z)).
and df %>% unnest(c(x, y, z)).
If you previously created new variable in unnest() you'll now need to
do it explicitly with mutate(). Convert df %>% unnest(y = fun(x, y, z))
to df %>% mutate(y = fun(x, y, z)) %>% unnest(y).
|
.key |
:
No longer needed because of the new new_col = c(col1, col2, col3) syntax.
|
data |
A data frame.
|
cols |
<tidy-select> Columns to unnest.
If you unnest() multiple columns, parallel entries must be of
compatible sizes, i.e. they're either equal or length 1 (following the
standard tidyverse recycling rules).
|
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 NULL or an empty data frame), that entire row
will be dropped from the output. If you want to preserve all rows,
use keep_empty = TRUE to replace size-0 elements with a single row
of missing values.
|
ptype |
Optionally, a named list of column name-prototype pairs to
coerce cols to, overriding the default that will be guessed from
combining the individual values. Alternatively, a single empty ptype
can be supplied, which will be applied to all cols.
|
names_sep, .names_sep |
If NULL, the default, the names will be left
as is. In nest(), inner names will come from the former outer names;
in unnest(), the new outer names will come from the inner names.
If a string, the inner and outer names will be used together. In
unnest(), the names of the new outer columns will be formed by pasting
together the outer and the inner column names, separated by names_sep. In
nest(), the new inner names will have the outer names + names_sep
automatically stripped. This makes names_sep roughly symmetric between
nesting and unnesting.
|
names_repair |
Used to check that output data frame has valid
names. Must be one of the following options:
"minimal": no name repair or checks, beyond basic existence,
"unique": make sure names are unique and not empty,
"check_unique": (the default), no name repair, but check they are unique,
"universal": make the names unique and syntactic
a function: apply custom name repair.
-
tidyr_legacy: use the name repair from tidyr 0.8.
a formula: a purrr-style anonymous function (see rlang::as_function())
See vctrs::vec_as_names() for more details on these terms and the
strategies used to enforce them.
|
.drop, .preserve |
:
all list-columns are now preserved; If there are any that you
don't want in the output use select() to remove them prior to
unnesting.
|
.id |
:
convert df %>% unnest(x, .id = "id") to df %>% mutate(id = names(x)) %>% unnest(x)).
|
.sep |
:
use names_sep instead.
|
Value
.data with nested or unnested columns. If the spectra column is
dropped or invalidated (see ir_new_ir()), the ir class is dropped, else
the object is of class ir.
Source
tidyr::nest()
See Also
Other tidyverse:
arrange.ir(),
distinct.ir(),
extract.ir(),
filter-joins,
filter.ir(),
group_by,
mutate-joins,
mutate,
pivot_longer.ir(),
pivot_wider.ir(),
rename,
rowwise.ir(),
select.ir(),
separate.ir(),
separate_rows.ir(),
slice,
summarize,
unite.ir()
Examples
## nest
ir_sample_data %>%
tidyr::nest(
contents = c(holocellulose, klason_lignin)
)
## unnest
ir_sample_data %>%
tidyr::nest(
contents = c(holocellulose, klason_lignin)
) %>%
tidyr::unnest("contents")
[Package
ir version 0.2.1
Index]