markfairbanks / tidytable

Tidy interface to 'data.table'
https://markfairbanks.github.io/tidytable/
Other
449 stars 33 forks source link

".drop = F" in group_by() #820

Open yohei-h opened 3 weeks ago

yohei-h commented 3 weeks ago

I would like to display all levels in a factor variable using group_by(, .drop = F), but I cannot with tidytable::group_by(). Below is a sample code.

d <- iris
library(tidyverse)
library(tidytable)

# I want to calculate the number of "XXX"
# -------------------------------------------------------
d %>% 
    mutate(Species = factor(Species, levels = c("setosa", "versicolor", "virginica", "XXX"))) %>% 
    dplyr::group_by(Species, .drop = F) %>% 
    dplyr::summarise(n = dplyr::n())

# But I cannot implement this using tidytable::group_by()
# -------------------------------------------------------
d %>% 
    mutate(Species = factor(Species, levels = c("setosa", "versicolor", "virginica", "XXX"))) %>% 
    tidytable::group_by(Species) %>% 
    tidytable::summarise(n = tidytable::n())

Is there a similar function in the tidytable package?