#' Title
#'
#' @inheritParams pivot_count
#'
#' @return
#' @export
#'
#' @examples
#' tidy_titanic %>% pivot_prop(rows = sex, cols = survived, within = sex)
pivot_prop <- function(data, rows = NULL, cols = NULL,
value = NULL, fun = sum,
within = NULL, pivot = T,
percent = T, round = F){
cols_quo <- rlang::enquo(cols)
value_quo <- rlang::enquo(value)
if(rlang::quo_is_null(value_quo)){
data <- data %>% dplyr::mutate(value = 1)
}else{
data <- data %>%
dplyr::mutate(value = fun({{value}}))
}
data %>%
dplyr::group_by(across(c({{rows}}, {{cols}})), .drop = FALSE) %>%
dplyr::summarize(value = fun(value)) %>%
dplyr::group_by(across(c({{within}}))) %>%
dplyr::mutate(prop = (value/sum(value)*ifelse(percent, 100, 1)) %>% round(1)) %>%
dplyr::select(-value) %>%
dplyr::ungroup() ->
tidy
if(pivot){
tidy %>%
tidyr::pivot_wider(values_from = prop, names_from = {{cols}})
}else{
tidy
}
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.