#' Counts number of times each digit occurs
#'
#' The 'count_digits()' function takes a numeric column of a data frame and
#' counts the number of times that each digit occurs in the first decimal place.
#'
#' @param data A data frame
#' @param variable A numeric variable that includes the first decimal place.
#' @param group A second variable used to group the primary variable such
#' that average frequency is calculated separately for each group.
#' @param decimal_place The decimal place for which digits are counted. The
#' default is set to one but any place may be specified by numeric rank,
#' i.e. "1" for the 1st decimal (tenths), "2" for the 2nd decimal (hundreds), etc.
#'
#' @return A tibble
#' @export
#'
#' @examples
#' count_digits(simulated_normal, obs, group)
#'
#' @importFrom magrittr %>%
#' @importFrom rlang .data
count_digits <- function(data, variable, group = NULL, decimal_place = 1) {
output <- data %>%
dplyr::select({{ variable }}, {{ group }}) %>%
dplyr::filter(!is.na({{ variable }})) %>%
dplyr::mutate(var = trunc({{ variable }} * 10^{{ decimal_place }}),
decimal = stringr::str_sub(.data$var, start = - {{ decimal_place }})) %>%
dplyr::group_by({{ group }}, .data$decimal ) %>%
dplyr::summarise(n = dplyr::n()) %>%
tidyr::pivot_wider(names_from = .data$decimal,
values_from = .data$n,
names_prefix = "n_") %>%
dplyr::mutate(dplyr::across(dplyr::starts_with("n_"), ~ifelse(is.na(.x), 0, .x)))
#This add a column for any digits that did not appear.
for(i in 0:9) {
#x <- paste("n_", i, sep = "")
if(!paste("n_", i, sep = "") %in% colnames(output)) {
output[[paste("n_", i, sep = "")]] <- 0
}
}
output %>%
dplyr::select(order(colnames(.)))
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.