Subset columns in nested data frames using their names and types
Source:R/nest_select.R
nest_select.Rd
nest_select()
selects (and optionally renames) variables in nested data
frames, using a concise mini-language that makes it easy to refer to
variables based on their name (e.g., a:f
selects all columns from a
on
the left to f
on the right). You can also use predicate functions like
is.numeric to select variables based on their properties.
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
- ...
One or more unquoted expressions separated by commas. Variable names can be used if they were positions in the data frame, so expressions like
x:y
can be used to select a range of variables.
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 affect.
Output columns are a subset of input columns, potentially with a different order. Columns will be renamed if
new_name = old_name
form is used.Data frame attributes are preserved.
Groups are maintained; you can't select off grouping variables.
Details
nest_select()
is largely a wrapper for dplyr::select()
and maintains the
functionality of select()
within each nested data frame. For more
information on select()
, please refer to the documentation in
dplyr
.
See also
Other single table verbs:
nest_arrange()
,
nest_filter()
,
nest_mutate()
,
nest_rename()
,
nest_slice()
,
nest_summarise()
Examples
gm_nest <- gapminder::gapminder %>% tidyr::nest(country_data = -continent)
gm_nest %>% nest_select(country_data, country, year, pop)
#> # A tibble: 5 × 2
#> continent country_data
#> <fct> <list>
#> 1 Asia <tibble [396 × 3]>
#> 2 Europe <tibble [360 × 3]>
#> 3 Africa <tibble [624 × 3]>
#> 4 Americas <tibble [300 × 3]>
#> 5 Oceania <tibble [24 × 3]>
gm_nest %>% nest_select(country_data, dplyr::where(is.numeric))
#> # A tibble: 5 × 2
#> continent country_data
#> <fct> <list>
#> 1 Asia <tibble [396 × 4]>
#> 2 Europe <tibble [360 × 4]>
#> 3 Africa <tibble [624 × 4]>
#> 4 Americas <tibble [300 × 4]>
#> 5 Oceania <tibble [24 × 4]>