Skip to contents

nest_replace_na() is used to replace missing values in selected columns of nested data frames using values specified by column.

Usage

nest_replace_na(.data, .nest_data, replace, ...)

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

replace

A list of values, with one value for each column in that has NA values to be replaced.

...

Additional arguments for tidyr::replace_na() methods. Currently unused.

Value

An object of the same type as .data. Each object in the column .nest_data

will have NAs replaced in the specified columns.

Details

nest_replace_na() is a wrapper for tidyr::replace_na() and maintains the functionality of replace_na() within each nested data frame. For more information on replace_na() please refer to the documentation in 'tidyr'.

See also

Examples

set.seed(123)
gm <- 
  gapminder::gapminder %>% 
  dplyr::mutate(pop = dplyr::if_else(runif(dplyr::n()) >= 0.9,
                                     NA_integer_,
                                     pop))
                                     
gm_nest <- gm %>% tidyr::nest(country_data = -continent)

gm_nest %>% 
  nest_replace_na(.nest_data = country_data,
                  replace = list(pop = -500))
#> # 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]>