Subset columns in nested data frames using their names and typesSource:
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
the left to
f on the right). You can also use predicate functions like
is.numeric to select variables based on their properties.
A data frame, data frame extension (e.g., a tibble), or a lazy data frame (e.g., from dbplyr or dtplyr).
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:ycan be used to select a range of variables.
An object of the same type as
.data. Each object in the column
will also be of the same type as the input. Each object in
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_nameform is used.
Data frame attributes are preserved.
Groups are maintained; you can't select off grouping variables.
nest_select() is largely a wrapper for
dplyr::select() and maintains the
select() within each nested data frame. For more
select(), please refer to the documentation in
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, 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]>