Created
June 14, 2022 06:22
-
-
Save b-rodrigues/3f4eac56dec315d9f0ef1df9063d7e84 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
get_counts <- function(dataset, var){ | |
dataset %>% | |
group_by(am) %>% | |
mutate("change_{{var}}" := n_distinct({{var}})) %>% | |
mutate(total = across(starts_with("change"), identity)) %>% | |
mutate(total = unlist(total)) %>% | |
filter(total == 11) %>% | |
select(-total) | |
} | |
get_counts(mtcars, hp) |
@b-rodrigues this works, but I'm not entirely sure it's the most efficient way:
get_counts <- function(dataset, var) { var <- rlang::enexpr(var) new_var <- glue::glue("change_{eval(quote(var))}") dataset %>% dplyr::group_by(am) %>% dplyr::mutate({{new_var}} := dplyr::n_distinct({{var}})) %>% dplyr::filter(.data[[new_var]] == 11) }
This is quite nice, and more efficient than my "solution"! Thanks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@b-rodrigues this works, but I'm not entirely sure it's the most efficient way: