nest_relocate()
changes column positions within a nested data frame, using
the same syntax as nest_select()
or dplyr::select()
to make it easy to
move blocks of columns at once.
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
- ...
Columns to move.
- .before, .after
Destination of columns selected by
...
. Supplying neither will move columns to the left-hand side; specifying both is an error.
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.
The same columns appear in the output, but (usually) in a different place.
Data frame attributes are preserved.
Groups are not affected.
Details
nest_relocate()
is largely a wrapper for dplyr::relocate()
and maintains
the functionality of relocate()
within each nested data frame. For more
information on relocate()
, please refer to the documentation in
dplyr
.
Examples
gm_nest <- gapminder::gapminder %>% tidyr::nest(country_data = -continent)
gm_nest %>% nest_relocate(country_data, year)
#> # 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_relocate(country_data, pop, .after = year)
#> # 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]>