Skip to contents

nest_count() lets you quickly count the unique values of one or more variables within each nested data frame. nest_count() results in a summary with one row per each set of variables to count by. nest_add_count() is equivalent with the exception that it retains all rows and adds a new column with group-wise counts.

Usage

nest_count(.data, .nest_data, ..., wt = NULL, sort = FALSE, name = NULL)

nest_add_count(.data, .nest_data, ..., wt = NULL, sort = FALSE, name = NULL)

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

...

Variables to group by.

wt

Frequency weights. Can be NULL or a variable:

  • If NULL (the default), counts the number of rows in each group.

  • If a variable, computes sum(wt) for each group.

sort

If TRUE, will show the largest groups at the top.

name

The name of the new column in the output.

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. nest_count() and nest_add_count() group each object in .nest_data transiently, so the output returned in .nest_data will have the same groups as the input.

Details

nest_count() and nest_add_count() are largely wrappers for dplyr::count() and dplyr::add_count() and maintain the functionality of count() and add_count() within each nested data frame. For more information on count() and add_count(), please refer to the documentation in dplyr.

Examples

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

# count the number of times each country appears in each nested tibble
gm_nest %>% nest_count(country_data, country)
#> # A tibble: 5 × 2
#>   continent country_data     
#>   <fct>     <list>           
#> 1 Asia      <tibble [33 × 2]>
#> 2 Europe    <tibble [30 × 2]>
#> 3 Africa    <tibble [52 × 2]>
#> 4 Americas  <tibble [25 × 2]>
#> 5 Oceania   <tibble [2 × 2]> 
gm_nest %>% nest_add_count(country_data, country)
#> # A tibble: 5 × 2
#>   continent country_data      
#>   <fct>     <list>            
#> 1 Asia      <tibble [396 × 6]>
#> 2 Europe    <tibble [360 × 6]>
#> 3 Africa    <tibble [624 × 6]>
#> 4 Americas  <tibble [300 × 6]>
#> 5 Oceania   <tibble [24 × 6]> 

# count the sum of population for each country in each nested tibble
gm_nest %>% nest_count(country_data, country, wt = pop)
#> # A tibble: 5 × 2
#>   continent country_data     
#>   <fct>     <list>           
#> 1 Asia      <tibble [33 × 2]>
#> 2 Europe    <tibble [30 × 2]>
#> 3 Africa    <tibble [52 × 2]>
#> 4 Americas  <tibble [25 × 2]>
#> 5 Oceania   <tibble [2 × 2]> 
gm_nest %>% nest_add_count(country_data, country, wt = pop)
#> # A tibble: 5 × 2
#>   continent country_data      
#>   <fct>     <list>            
#> 1 Asia      <tibble [396 × 6]>
#> 2 Europe    <tibble [360 × 6]>
#> 3 Africa    <tibble [624 × 6]>
#> 4 Americas  <tibble [300 × 6]>
#> 5 Oceania   <tibble [24 × 6]>