nest_rename()
changes the names of individual variables using
new_name = old_name
syntax; nest_rename_with()
renames columns using a
function.
Usage
nest_rename(.data, .nest_data, ...)
nest_rename_with(.data, .nest_data, .fn, .cols = dplyr::everything(), ...)
Arguments
- .data
A data frame, data frame extension (e.g., a tibble), or a lazy data frame (e.g., from dbplyr or dtplyr).
- .nest_data
A list-column containing data frames
- ...
For
nest_rename()
: Usenew_name = old_name
to rename selected variables.For
nest_rename_with()
: additional arguments passed onto.fn
.- .fn
A function used to transform the selected
.cols
. Should return a character vector the same length as the input.- .cols
Columns to rename; defaults to all columns.
Value
An object of the same type as .data
. Each object in the column .nest_data
will also be of the same type as the input. Each object in .nest_data
has
the following properties:
Rows are not affected.
Column names are changed; column order is preserved.
Data frame attributes are preserved.
Groups are updated to reflect new names.
Details
nest_rename()
and nest_rename_with()
are largely wrappers for
dplyr::rename()
and dplyr::rename_with()
and maintain the functionality
of rename()
and rename_with()
within each nested data frame. For more
information on rename()
or rename_with()
, please refer to the
documentation in dplyr
.
See also
Other single table verbs:
nest_arrange()
,
nest_filter()
,
nest_mutate()
,
nest_select()
,
nest_slice()
,
nest_summarise()
Examples
gm_nest <- gapminder::gapminder %>% tidyr::nest(country_data = -continent)
gm_nest %>% nest_rename(country_data, population = pop)
#> # A tibble: 5 × 2
#> continent country_data
#> <fct> <list>
#> 1 Asia <tibble [396 × 5]>
#> 2 Europe <tibble [360 × 5]>
#> 3 Africa <tibble [624 × 5]>
#> 4 Americas <tibble [300 × 5]>
#> 5 Oceania <tibble [24 × 5]>
gm_nest %>% nest_rename_with(country_data, stringr::str_to_lower)
#> # A tibble: 5 × 2
#> continent country_data
#> <fct> <list>
#> 1 Asia <tibble [396 × 5]>
#> 2 Europe <tibble [360 × 5]>
#> 3 Africa <tibble [624 × 5]>
#> 4 Americas <tibble [300 × 5]>
#> 5 Oceania <tibble [24 × 5]>