Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create all group combinations, fill with zero if no observations #2

Open
EvaMaeRey opened this issue Apr 6, 2022 · 0 comments
Open

Comments

@EvaMaeRey
Copy link
Owner

pivot_count <- function(data, cols = NULL, rows = NULL, pivot = T){

cols_quo <- rlang::enquo(cols)

tidy <- data %>%
dplyr::group_by(dplyr::across(c({{cols}}, {{rows}})), .drop = FALSE) %>%
dplyr::summarize(value = dplyr::n()) %>%
# tidyr::complete(dplyr::across(c({{cols}}, {{rows}}))) %>% # <- It would be nice if this bit worked but across only works in dplyr verbs. complete is tidyr verb
dplyr::mutate(value = tidyr::replace_na(.data$value, 0)) %>%
dplyr::arrange(dplyr::across(c({{rows}}, {{cols}}))) %>%
dplyr::ungroup()

do not pivot if argument pivot false or no columns specified

if(pivot == F | rlang::quo_is_null(cols_quo)){

tidy

otherwise pivot by columns

}else{

tidy %>%
tidyr::pivot_wider(names_from = {{cols}})

}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant