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 formnew_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)
anddf %>% unnest(x, y, z)
. Convert todf %>% nest(data = c(x, y, z))
. anddf %>% unnest(c(x, y, z))
.If you previously created new variable in
unnest()
you'll now need to do it explicitly withmutate()
. Convertdf %>% unnest(y = fun(x, y, z))
todf %>% 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, usekeep_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 allcols
.- names_sep, .names_sep
If
NULL
, the default, the names will be left as is. Innest()
, inner names will come from the former outer names; inunnest()
, 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 bynames_sep
. Innest()
, the new inner names will have the outer names +names_sep
automatically stripped. This makesnames_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")
todf %>% mutate(id = names(x)) %>% unnest(x))
.- .sep
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
.
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)
)
#> # A tibble: 58 × 6
#> id_measurement id_sample sample_type sample_comment spectra contents
#> * <int> <chr> <chr> <chr> <named > <list>
#> 1 1 GN 11-389 needles Abies Firma Momi fir <tibble> <tibble>
#> 2 2 GN 11-400 needles Cupressocyparis leyla… <tibble> <tibble>
#> 3 3 GN 11-407 needles Juniperus chinensis C… <tibble> <tibble>
#> 4 4 GN 11-411 needles Metasequoia glyptostr… <tibble> <tibble>
#> 5 5 GN 11-416 needles Pinus strobus Torulosa <tibble> <tibble>
#> 6 6 GN 11-419 needles Pseudolarix amabili G… <tibble> <tibble>
#> 7 7 GN 11-422 needles Sequoia sempervirens … <tibble> <tibble>
#> 8 8 GN 11-423 needles Taxodium distichum Ca… <tibble> <tibble>
#> 9 9 GN 11-428 needles Thuja occidentalis Ea… <tibble> <tibble>
#> 10 10 GN 11-434 needles Tsuga caroliniana Car… <tibble> <tibble>
#> # … with 48 more rows
## unnest
ir_sample_data %>%
tidyr::nest(
contents = c(holocellulose, klason_lignin)
) %>%
tidyr::unnest("contents")
#> # A tibble: 58 × 7
#> id_measurement id_sample sample_type sample_comment spectra holocellulose
#> * <int> <chr> <chr> <chr> <named > [1]
#> 1 1 GN 11-389 needles Abies Firma Momi… <tibble> 0.308
#> 2 2 GN 11-400 needles Cupressocyparis … <tibble> 0.250
#> 3 3 GN 11-407 needles Juniperus chinen… <tibble> 0.336
#> 4 4 GN 11-411 needles Metasequoia glyp… <tibble> 0.184
#> 5 5 GN 11-416 needles Pinus strobus To… <tibble> 0.309
#> 6 6 GN 11-419 needles Pseudolarix amab… <tibble> 0.335
#> 7 7 GN 11-422 needles Sequoia sempervi… <tibble> 0.241
#> 8 8 GN 11-423 needles Taxodium distich… <tibble> 0.125
#> 9 9 GN 11-428 needles Thuja occidental… <tibble> 0.252
#> 10 10 GN 11-434 needles Tsuga carolinian… <tibble> 0.349
#> # … with 48 more rows, and 1 more variable: klason_lignin [1]