Say I have the following tibble:
people <- tibble::tribble(
~name, ~gender, ~state_residence, ~city_residence,
"Dan", "Male", "Indiana", "Indianapolis",
"Jerry", "Male", "New York", "New York City",
"Bojack", "Male", "California", "Los Angeles",
"Leslie", "Female", "Indiana", "Pawnee",
"Liz", "Female", "New York", "New York City",
"Jesse", "Male", "California", "San Francisco",
"Daphne", "Female", "Washington", "Seattle"
)
And I've defined the following function:
count_and_percent <- function(tbl, var_obj, var_name) {
tbl %>%
group_by({{var_obj}}) %>%
summarize(count = n()) %>%
mutate(
variable = var_name,
percent = round(
(count/nrow(tbl)*100),
digits = 2
)
) %>%
rename(
category = {{var_obj}}
)
}
So for example I get:
> count_and_percent(people, state_residence, "state_residence")
# A tibble: 4 × 4
category count variable percent
<chr> <int> <chr> <dbl>
1 California 2 state_residence 28.6
2 Indiana 2 state_residence 28.6
3 New York 2 state_residence 28.6
4 Washington 1 state_residence 14.3
Question 1: Is there a way to modify the function so that the var_obj and var_name arguments can be combined into one argument?
Questions 2: If I want to apply the count_and_percent function across multiple columns in the same dataframe, could you illustrate how to do that with one of the purrr functions? Still trying to wrap my head around this stuff.