rsquaredacademy / descriptr
 1 ```#' @title Multiple One & Two Way Tables ``` 2 ```#' @description \code{ds_auto_freq_table} creates multiple one way tables by creating ``` 3 ```#' a frequency table for each categorical variable in a data frame. ``` 4 ```#' \code{ds_auto_cross_table} creates multiple two way tables by creating a cross ``` 5 ```#' table for each unique pair of categorical variables in a data frame. ``` 6 ```#' @param data A \code{data.frame} or \code{tibble}. ``` 7 ```#' @param ... Column(s) in \code{data}. ``` 8 ```#' @details \code{ds_auto_freq_table} is a extension of the \code{ds_freq_table} ``` 9 ```#' function. It creates a frequency table for each categorical variable in the ``` 10 ```#' dataframe. \code{ds_auto_cross_table} is a extension of the \code{ds_cross_table} ``` 11 ```#' function. It creates a two way table for each unique pair of categorical ``` 12 ```#' variables in the dataframe. ``` 13 ```#' @section Deprecated Functions: ``` 14 ```#' \code{ds_oway_tables()} and \code{ds_tway_tables()} have been deprecated. ``` 15 ```#' Instead use \code{ds_auto_freq_table()} and \code{ds_auto_cross_table()}. ``` 16 ```#' @examples ``` 17 ```#' # frequency table for all columns ``` 18 ```#' ds_auto_freq_table(mtcarz) ``` 19 ```#' ``` 20 ```#' # frequency table for multiple columns ``` 21 ```#' ds_auto_freq_table(mtcarz, cyl, gear) ``` 22 ```#' ``` 23 ```#' # cross table for all columns ``` 24 ```#' ds_auto_cross_table(mtcarz) ``` 25 ```#' ``` 26 ```#' # cross table for multiple columns ``` 27 ```#' ds_auto_cross_table(mtcarz, cyl, gear, am) ``` 28 ```#' ``` 29 ```#' @seealso \code{link{ds_freq_table}} \code{link{ds_cross_table}} ``` 30 ```#' @export ``` 31 ```#' ``` 32 ```ds_auto_freq_table <- function(data, ...) { ``` 33 34 1 ``` check_df(data) ``` 35 1 ``` var <- rlang::quos(...) ``` 36 1 ``` is_factor <- sapply(data, is.factor) ``` 37 38 1 ``` if (length(var) < 1) { ``` 39 1 ``` if (!any(is_factor == TRUE)) { ``` 40 1 ``` stop("Data has no categorical variables.", call. = FALSE) ``` 41 ``` } ``` 42 1 ``` plot_data <- data[is_factor] ``` 43 ``` } else { ``` 44 1 ``` data %<>% ``` 45 1 ``` dplyr::select(!!! var) ``` 46 1 ``` is_factor <- sapply(data, is.factor) ``` 47 1 ``` if (!any(is_factor == TRUE)) { ``` 48 1 ``` stop("Data has no categorical variables.", call. = FALSE) ``` 49 ``` } ``` 50 0 ``` plot_data <- data[is_factor] ``` 51 ``` } ``` 52 53 1 ``` if (ncol(plot_data) < 1) { ``` 54 0 ``` stop("Data has no categorical variables.", call. = FALSE) ``` 55 ``` } ``` 56 57 1 ``` factor_var <- names(plot_data) ``` 58 1 ``` n <- length(factor_var) ``` 59 60 1 ``` for (i in seq_len(n)) { ``` 61 1 ``` k <- freq_table2(plot_data[i], factor_var[i]) ``` 62 1 ``` print(k) ``` 63 ``` } ``` 64 65 ```} ``` 66 67 68 69 ```#' @rdname ds_auto_freq_table ``` 70 ```#' @export ``` 71 ```#' ``` 72 ```ds_auto_cross_table <- function(data, ...) { ``` 73 74 1 ``` check_df(data) ``` 75 1 ``` var <- rlang::quos(...) ``` 76 1 ``` is_factor <- sapply(data, is.factor) ``` 77 78 1 ``` if (length(var) < 1) { ``` 79 1 ``` if (!any(is_factor == TRUE)) { ``` 80 1 ``` stop("Data has no categorical variables.", call. = FALSE) ``` 81 ``` } ``` 82 1 ``` plot_data <- data[is_factor] ``` 83 ``` } else { ``` 84 1 ``` data %<>% ``` 85 1 ``` dplyr::select(!!! var) ``` 86 1 ``` is_factor <- sapply(data, is.factor) ``` 87 1 ``` if (!any(is_factor == TRUE)) { ``` 88 1 ``` stop("Data has no categorical variables.", call. = FALSE) ``` 89 ``` } ``` 90 1 ``` if (length(is_factor) < 2) { ``` 91 1 ``` stop("Two way table requires at least 2 categorical variables.", call. = FALSE) ``` 92 ``` } else { ``` 93 1 ``` plot_data <- data[is_factor] ``` 94 ``` } ``` 95 ``` } ``` 96 97 1 ``` if (ncol(plot_data) < 1) { ``` 98 0 ``` stop("Data has no categorical variables.", call. = FALSE) ``` 99 ``` } ``` 100 101 1 ``` factor_var <- names(plot_data) ``` 102 1 ``` factor_start <- combn(factor_var, 2) ``` 103 1 ``` n <- dim(factor_start)[2] ``` 104 105 1 ``` cat( ``` 106 1 ``` formatter(" Cell Contents\n"), ``` 107 1 ``` "|---------------|\n", ``` 108 1 ``` "|", formatter("Frequency"), "|\n", ``` 109 1 ``` "|", formatter("Percent"), "|\n", ``` 110 1 ``` "|", formatter("Row Pct"), "|\n", ``` 111 1 ``` "|", formatter("Col Pct"), "|\n", ``` 112 1 ``` "|---------------|\n\n", ``` 113 1 ``` "Total Observations: ", nrow(data), "\n\n" ``` 114 ``` ) ``` 115 116 1 ``` for (i in seq_len(n)) { ``` 117 1 ``` k <- cross_table2(plot_data[[factor_start[, i][1]]], plot_data[[factor_start[, i][2]]], ``` 118 1 ``` factor_start[, i][1], factor_start[, i][2]) ``` 119 1 ``` print(k) ``` 120 ``` } ``` 121 ```} ``` 122 123 ```#' @export ``` 124 ```#' @rdname ds_auto_freq_table ``` 125 ```#' @usage NULL ``` 126 ```#' ``` 127 ```ds_tway_tables <- function(data) { ``` 128 0 ``` .Deprecated("ds_auto_cross_table()") ``` 129 0 ``` ds_auto_cross_table(data) ``` 130 ```} ``` 131 132 ```#' @export ``` 133 ```#' @rdname ds_auto_freq_table ``` 134 ```#' @usage NULL ``` 135 ```#' ``` 136 ```ds_oway_tables <- function(data) { ``` 137 0 ``` .Deprecated("ds_auto_freq_table()") ``` 138 0 ``` ds_auto_freq_table(data) ``` 139 ```} ```

Read our documentation on viewing source code .

Loading