Navigation Menu

Skip to content

Commit

Permalink
closes #83
Browse files Browse the repository at this point in the history
  • Loading branch information
aravindhebbali committed Jan 18, 2019
1 parent fff0f8b commit dc9f8cd
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 67 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Expand Up @@ -32,8 +32,10 @@ export(dist_norm_prob)
export(dist_t_perc)
export(dist_t_plot)
export(dist_t_prob)
export(ds_auto_cross_table)
export(ds_auto_group_summary)
export(ds_auto_summary)
export(ds_auto_tabulation)
export(ds_cross_table)
export(ds_css)
export(ds_cvar)
Expand Down
113 changes: 83 additions & 30 deletions R/ds-mult-table.R
@@ -1,63 +1,101 @@
#' @title Multiple One & Two Way Tables
#' @description \code{ds_oway_tables} creates multiple one way tables by creating
#' @description \code{ds_auto_tabulation} creates multiple one way tables by creating
#' a frequency table for each categorical variable in a data frame.
#' \code{ds_tway_tables} creates multiple two way tables by creating a cross
#' \code{ds_auto_cross_table} creates multiple two way tables by creating a cross
#' table for each unique pair of categorical variables in a data frame.
#' @param data a data frame
#' @details \code{ds_oway_tables} is a extension of the \code{ds_freq_table}
#' @param data A \code{data.frame} or \code{tibble}.
#' @param ... Column(s) in \code{data}.
#' @details \code{ds_auto_tabulation} is a extension of the \code{ds_freq_table}
#' function. It creates a frequency table for each categorical variable in the
#' dataframe. \code{ds_tway_tables} is a extension of the \code{ds_cross_table}
#' dataframe. \code{ds_auto_cross_table} is a extension of the \code{ds_cross_table}
#' function. It creates a two way table for each unique pair of categorical
#' variables in the dataframe.
#' @section Deprecated Functions:
#' \code{ds_oway_tables()} and \code{ds_tway_tables()} have been deprecated.
#' Instead use \code{ds_auto_tabulation()} and \code{ds_auto_cross_table()}.
#' @examples
#' # multiple one way tables
#' ds_oway_tables(mtcarz)
#' ds_auto_tabulation(mtcarz)
#' ds_auto_tabulation(mtcarz, cyl, gear)
#'
#' # multiple two way tables
#' ds_tway_tables(mtcarz)
#' ds_auto_cross_table(mtcarz)
#' ds_auto_cross_table(mtcarz, cyl, gear, am)
#' @seealso \code{link{ds_freq_table}} \code{link{ds_cross_table}}
#' @export
#'
ds_oway_tables <- function(data) {
ds_auto_tabulation <- function(data, ...) {

check_df(data)
var <- rlang::quos(...)
is_factor <- sapply(data, is.factor)

is.fact <- sapply(data, is.factor)
if (length(var) < 1) {
if (!any(is_factor == TRUE)) {
rlang::abort("Data has no categorical variables.")
}
plot_data <- data[is_factor]
} else {
data %<>%
dplyr::select(!!! var)
is_factor <- sapply(data, is.factor)
if (!any(is_factor == TRUE)) {
rlang::abort("Data has no categorical variables.")
}
plot_data <- data[is_factor]
}

if (!any(is.fact == TRUE)) {
stop("data has no factor variables")
if (ncol(plot_data) < 1) {
rlang::abort("Data has no categorical variables.")
}

factors.df <- data[, is.fact]
nam <- names(factors.df)
nc <- ncol(factors.df)
factor_var <- names(plot_data)
n <- length(factor_var)

for (i in seq_len(nc)) {
k <- freq_table2(factors.df[i], nam[i])
for (i in seq_len(n)) {
k <- freq_table2(plot_data[i], factor_var[i])
print(k)
}

}



#' @rdname ds_oway_tables
#' @rdname ds_auto_tabulation
#' @export
#'
ds_tway_tables <- function(data) {
ds_auto_cross_table <- function(data, ...) {

check_df(data)
var <- rlang::quos(...)
is_factor <- sapply(data, is.factor)

is.fact <- sapply(data, is.factor)
if (length(var) < 1) {
if (!any(is_factor == TRUE)) {
rlang::abort("Data has no categorical variables.")
}
plot_data <- data[is_factor]
} else {
data %<>%
dplyr::select(!!! var)
is_factor <- sapply(data, is.factor)
if (!any(is_factor == TRUE)) {
rlang::abort("Data has no categorical variables.")
}
if (length(is_factor) < 2) {
rlang::abort("Stacked bar plot requires 2 categorical variables.")
} else {
plot_data <- data[is_factor]
}
}

if (sum(is.fact) < 2) {
stop("data must have at least two factor variables")
if (ncol(plot_data) < 1) {
rlang::abort("Data has no categorical variables.")
}

factors.df <- data[, is.fact]
nc <- ncol(factors.df)
nam <- names(factors.df)
n <- nc - 1
factor_var <- names(plot_data)
factor_start <- combn(factor_var, 2)
n <- dim(factor_start)[2]

cat(
formatter(" Cell Contents\n"),
Expand All @@ -71,11 +109,26 @@ ds_tway_tables <- function(data) {
)

for (i in seq_len(n)) {
p <- i + 1
for (j in p:nc) {
k <- cross_table2(factors.df[[i]], factors.df[[j]], nam[i], nam[j])
print(k)
}
k <- cross_table2(plot_data[[factor_start[, i][1]]], plot_data[[factor_start[, i][2]]],
factor_start[, i][1], factor_start[, i][2])
print(k)
}
}

#' @export
#' @rdname ds_auto_tabulation
#' @usage NULL
#'
ds_tway_tables <- function(data) {
.Deprecated("ds_auto_cross_table()")
ds_auto_cross_table(data)
}

#' @export
#' @rdname ds_auto_tabulation
#' @usage NULL
#'
ds_oway_tables <- function(data) {
.Deprecated("ds_auto_tabulation()")
ds_auto_tabulation(data)
}
49 changes: 49 additions & 0 deletions man/ds_auto_tabulation.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 0 additions & 37 deletions man/ds_oway_tables.Rd

This file was deleted.

0 comments on commit dc9f8cd

Please sign in to comment.