1 ```#' Descriptive statistics and frquency tables ``` 2 ```#' ``` 3 ```#' Generate summary statistics & frequency table for all continuous variables in data. ``` 4 ```#' ``` 5 ```#' @param data A \code{data.frame} or \code{tibble}. ``` 6 ```#' @param ... Column(s) in \code{data}. ``` 7 ```#' ``` 8 ```#' @examples ``` 9 ```#' # all columns ``` 10 ```#' ds_auto_summary_stats(mtcarz) ``` 11 ```#' ``` 12 ```#' # multiple columns ``` 13 ```#' ds_auto_summary_stats(mtcarz, disp, hp) ``` 14 ```#' ``` 15 ```#' @export ``` 16 ```#' ``` 17 ```ds_auto_summary_stats <- function(data, ...) { ``` 18 19 1 ``` check_df(data) ``` 20 1 ``` var <- rlang::quos(...) ``` 21 1 ``` is_num <- sapply(data, is.numeric) ``` 22 23 1 ``` if (length(var) < 1) { ``` 24 1 ``` if (!any(is_num == TRUE)) { ``` 25 1 ``` stop("Data has no continuous variables.", call. = FALSE) ``` 26 ``` } ``` 27 0 ``` plot_data <- data[is_num] ``` 28 ``` } else { ``` 29 1 ``` data %<>% ``` 30 1 ``` dplyr::select(!!! var) ``` 31 1 ``` is_num <- sapply(data, is.numeric) ``` 32 1 ``` if (!any(is_num == TRUE)) { ``` 33 1 ``` stop("Data has no continuous variables.", call. = FALSE) ``` 34 ``` } ``` 35 1 ``` plot_data <- data[is_num] ``` 36 ``` } ``` 37 38 1 ``` if (ncol(plot_data) < 1) { ``` 39 0 ``` stop("Data has no continuous variables.", call. = FALSE) ``` 40 ``` } ``` 41 42 1 ``` num_var <- names(plot_data) ``` 43 44 1 ``` for (i in num_var) { ``` 45 46 1 ``` ds_rule(paste0('Variable: ', i)) ``` 47 1 ``` cat('\n\n') ``` 48 1 ``` ds_rule(paste0('Summary Statistics')) ``` 49 1 ``` cat('\n\n') ``` 50 1 ``` print(ds_summary_stats(data, i)) ``` 51 1 ``` cat('\n\n') ``` 52 1 ``` ds_rule(paste0('Frequency Distribution')) ``` 53 1 ``` cat('\n\n') ``` 54 1 ``` print(ds_freq_table(data, i)) ``` 55 1 ``` cat('\n\n\n') ``` 56 ``` } ``` 57 58 ```} ``` 59 60 ```#' Tabulation ``` 61 ```#' ``` 62 ```#' Generate summary statistics for all continuous variables in data. ``` 63 ```#' ``` 64 ```#' @param data A \code{data.frame} or \code{tibble}. ``` 65 ```#' @param ... Column(s) in \code{data}. ``` 66 ```#' ``` 67 ```#' @examples ``` 68 ```#' # summary statistics of mpg & disp for each level of cyl & gear ``` 69 ```#' ds_auto_group_summary(mtcarz, cyl, gear, mpg, disp) ``` 70 ```#' ``` 71 ```#' @export ``` 72 ```#' ``` 73 ```ds_auto_group_summary <- function(data, ...) { ``` 74 75 1 ``` check_df(data) ``` 76 1 ``` var <- rlang::quos(...) ``` 77 78 1 ``` is_num <- sapply(data, is.numeric) ``` 79 1 ``` is_factor <- sapply(data, is.factor) ``` 80 81 1 ``` if (length(var) < 1) { ``` 82 1 ``` if (!any(is_factor == TRUE)) { ``` 83 1 ``` stop("Data has no categorical variables.", call. = FALSE) ``` 84 ``` } ``` 85 1 ``` if (!any(is_num == TRUE)) { ``` 86 1 ``` stop("Data has no continuous variables.", call. = FALSE) ``` 87 ``` } ``` 88 0 ``` plot_data <- cbind(data[is_factor] , data[is_num] ) ``` 89 ``` } else { ``` 90 1 ``` data %<>% ``` 91 1 ``` dplyr::select(!!! var) ``` 92 1 ``` is_num <- sapply(data, is.numeric) ``` 93 1 ``` if (!any(is_num == TRUE)) { ``` 94 1 ``` stop("Data has no continuous variables.", call. = FALSE) ``` 95 ``` } ``` 96 1 ``` is_factor <- sapply(data, is.factor) ``` 97 1 ``` if (!any(is_factor == TRUE)) { ``` 98 1 ``` stop("Data has no categorical variables.", call. = FALSE) ``` 99 ``` } ``` 100 1 ``` plot_data <- cbind(data[is_factor], data[is_num]) ``` 101 ``` } ``` 102 103 1 ``` if (ncol(data) < 1) { ``` 104 0 ``` stop("Data should include at least one categorical and one continuous variable.", call. = FALSE) ``` 105 ``` } ``` 106 107 1 ``` is_num <- sapply(plot_data, is.numeric) ``` 108 1 ``` is_factor <- sapply(plot_data, is.factor) ``` 109 1 ``` num_data <- plot_data[is_num] ``` 110 1 ``` fact_data <- plot_data[is_factor] ``` 111 1 ``` fact_var <- names(fact_data) ``` 112 1 ``` num_var <- names(num_data) ``` 113 1 ``` combs <- expand.grid(fact_var, num_var) ``` 114 1 ``` myplots <- list() ``` 115 1 ``` n <- nrow(combs) ``` 116 117 1 ``` for (i in seq_len(n)) { ``` 118 1 ``` print(ds_group_summary(data, !! sym(as.character(combs[i, 1])), !! sym(as.character(combs[i, 2])))) ``` 119 1 ``` cat('\n\n\n') ``` 120 ``` } ``` 121 ```} ```