nest_mutate: Create, modify, and delete columns in nested data frames

View source: R/nest_mutate.R

nest_mutateR Documentation

Create, modify, and delete columns in nested data frames

Description

nest_mutate() adds new variables to and preserves existing ones within the nested data frames in .nest_data. nest_transmute() adds new variables to and drops existing ones from the nested data frames in .nest_data.

Usage

nest_mutate(.data, .nest_data, ...)

nest_transmute(.data, .nest_data, ...)

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

...

Name-value pairs. The name gives the name of the column in the output.

The value can be:

  • A vector of length 1, which will be recycled to the correct length.

  • NULL, to remove the column.

  • A data frame or tibble, to create multiple columns in the output.

Details

nest_mutate() and nest_transmute() are largely wrappers for dplyr::mutate() and dplyr::transmute() and maintain the functionality of mutate() and transmute() within each nested data frame. For more information on mutate() or transmute(), please refer to the documentation in dplyr.

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:

  • For nest_mutate():

    • Columns from each object in .nest_data will be preserved according to the .keep argument.

    • Existing columns that are modified by ... will always be returned in their original location.

    • New columns created through ... will be placed according to the .before and .after arguments.

  • For nest_transmute():

    • Columns created or modified through ... will be returned in the order specified by ....

    • Unmodified grouping columns will be placed at the front.

  • The number of rows is not affected.

  • Columns given the value NULL will be removed.

  • Groups will be recomputed if a grouping variable is mutated.

  • Data frame attributes will be preserved.

See Also

Other single table verbs: nest_arrange(), nest_filter(), nest_rename(), nest_select(), nest_slice(), nest_summarise()

Examples

gm_nest <- gapminder::gapminder %>% tidyr::nest(country_data = -continent)

# add or modify columns:
gm_nest %>%
  nest_mutate(
    country_data,
    lifeExp = NULL,
    gdp = gdpPercap * pop,
    pop = pop/1000000
  )
  
# use dplyr::across() to apply transformation to multiple columns 
gm_nest %>%
  nest_mutate(
    country_data,
    across(c(lifeExp:gdpPercap), mean)
  )

# nest_transmute() drops unused columns when mutating:
gm_nest %>%
  nest_transmute(
    country_data,
    country = country,
    year = year,
    pop = pop/1000000
  )

nplyr documentation built on Feb. 16, 2023, 7:24 p.m.