Removes columns from the current list of groups, with the additional option to remove the columns from the data completely. In essence, the opposite of dplyr::group_by() with add = TRUE.

group_drop(.data, ..., .remove_dropped = FALSE)

Arguments

.data

A grouped tbl, tibble, or data.frame

...

Quoted or unquoted column names to be removed from the grouping

.remove_dropped

Should columns that are dropped from the grouping also be removed from .data? Default is FALSE.

See also

Other Group Utilities: with_retain_groups, with_ungroup

Examples

# Remove "type" from the groups tidyr::table2 %>% dplyr::group_by(country, year, type) %>% group_drop(type)
#> # A tibble: 12 x 4 #> # Groups: country, year [6] #> country year type count #> <chr> <int> <chr> <int> #> 1 Afghanistan 1999 cases 745 #> 2 Afghanistan 1999 population 19987071 #> 3 Afghanistan 2000 cases 2666 #> 4 Afghanistan 2000 population 20595360 #> 5 Brazil 1999 cases 37737 #> 6 Brazil 1999 population 172006362 #> 7 Brazil 2000 cases 80488 #> 8 Brazil 2000 population 174504898 #> 9 China 1999 cases 212258 #> 10 China 1999 population 1272915272 #> 11 China 2000 cases 213766 #> 12 China 2000 population 1280428583
# Remove "type" from the groups and the output data frame tidyr::table2 %>% dplyr::group_by(country, year, type) %>% group_drop(type, .remove_dropped = TRUE)
#> # A tibble: 12 x 3 #> # Groups: country, year [6] #> country year count #> <chr> <int> <int> #> 1 Afghanistan 1999 745 #> 2 Afghanistan 1999 19987071 #> 3 Afghanistan 2000 2666 #> 4 Afghanistan 2000 20595360 #> 5 Brazil 1999 37737 #> 6 Brazil 1999 172006362 #> 7 Brazil 2000 80488 #> 8 Brazil 2000 174504898 #> 9 China 1999 212258 #> 10 China 1999 1272915272 #> 11 China 2000 213766 #> 12 China 2000 1280428583
# Only columns that were dropped from groups will be removed tidyr::table2 %>% dplyr::group_by(country, type) %>% group_drop(year, type, .remove_dropped = TRUE)
#> # A tibble: 12 x 3 #> # Groups: country [3] #> country year count #> <chr> <int> <int> #> 1 Afghanistan 1999 745 #> 2 Afghanistan 1999 19987071 #> 3 Afghanistan 2000 2666 #> 4 Afghanistan 2000 20595360 #> 5 Brazil 1999 37737 #> 6 Brazil 1999 172006362 #> 7 Brazil 2000 80488 #> 8 Brazil 2000 174504898 #> 9 China 1999 212258 #> 10 China 1999 1272915272 #> 11 China 2000 213766 #> 12 China 2000 1280428583
# Nothing happens if trying to drop a group that's not in the groups tidyr::table2 %>% dplyr::group_by(country, year) %>% group_drop(type)
#> # A tibble: 12 x 4 #> # Groups: country, year [6] #> country year type count #> <chr> <int> <chr> <int> #> 1 Afghanistan 1999 cases 745 #> 2 Afghanistan 1999 population 19987071 #> 3 Afghanistan 2000 cases 2666 #> 4 Afghanistan 2000 population 20595360 #> 5 Brazil 1999 cases 37737 #> 6 Brazil 1999 population 172006362 #> 7 Brazil 2000 cases 80488 #> 8 Brazil 2000 population 174504898 #> 9 China 1999 cases 212258 #> 10 China 1999 population 1272915272 #> 11 China 2000 cases 213766 #> 12 China 2000 population 1280428583