From bfc1d958e3aad99900db103f1192da76ad681eb2 Mon Sep 17 00:00:00 2001 From: gklorfine Date: Thu, 12 Feb 2026 15:48:28 -0500 Subject: [PATCH 1/8] Added as_table() and as_array() to dev/ --- dev/as_array.R | 31 +++++++++++++++++++++++ dev/as_table.R | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 dev/as_array.R create mode 100644 dev/as_table.R diff --git a/dev/as_array.R b/dev/as_array.R new file mode 100644 index 0000000..d98012e --- /dev/null +++ b/dev/as_array.R @@ -0,0 +1,31 @@ +#' Convert frequency or case form data into array form +#' +#' Converts object (`obj`) in frequency or case form into array form. `freq` +#' must be supplied if `obj` is in frequency form. +#' +#' @param obj object to be converted to array form +#' @param freq If `obj` is in frequency form, this is the name of the frequency column. Leave as `NULL` if `obj` is in case form. +#' @param dims A character vector of table dimensions. If not specified, all variables apart from `freq` will be used as dimensions +#' @return object in array form +#' +#' @details +#' Unclasses the \code{as_table()} function to return an object in array form. +#' +#' @examples +#' \dontrun{ +#' freqForm <- as.data.frame(HairEyeColor) # Generate frequency form data +#' tidy_freqForm <- as_tibble(HairEyeColor) # Generate tidy frequency form data +#' caseForm <- expand.dft(freqForm) # Generate case form data +#' +#' as_array(freqForm, freq = "Freq") # frequency -> array form +#' as_array(freqForm) # Warned if forgot freq +#' as_array(caseForm) # case form -> array form +#' +#' # For specific dimensions +#' as_array(tidy_freqForm, freq = "n", dims = c("Hair", "Eye")) +#' } +#' @export + +as_array <- function(obj, freq = NULL, dims = NULL){ + return(unclass(as_table(obj, freq, dims))) # Unclass as_table output +} \ No newline at end of file diff --git a/dev/as_table.R b/dev/as_table.R new file mode 100644 index 0000000..a043839 --- /dev/null +++ b/dev/as_table.R @@ -0,0 +1,67 @@ +#' Convert frequency or case form data into table form +#' +#' Converts object (`obj`) in frequency or case form into table form. `freq` +#' must be supplied if `obj` is in frequency form. +#' +#' @param obj object to be converted to table form +#' @param freq If `obj` is in frequency form, this is the name of the frequency column. Leave as `NULL` if `obj` is in case form. +#' @param dims A list of table dimensions. If not specified, all variables apart from `freq` will be used as dimensions +#' @return object in table form +#' +#' @details +#' If `obj` was in table form to begin with, it is simply returned to the user +#' as-is. +#' +#' +#' @examples +#' \dontrun{ +#' data("HairEyeColor") +#' freqForm <- as.data.frame(HairEyeColor) # Generate frequency form data +#' tidy_freqForm <- as_tibble(HairEyeColor) # Generate tidy frequency form data +#' caseForm <- expand.dft(freqForm) # Generate case form data +#' +#' as_table(freqForm, freq = "Freq") # frequency -> table form +#' as_table(freqForm) # Warned if forgot freq +#' as_table(caseForm) # case form -> table form +#' +#' # For specific dimensions +#' as_table(tidy_freqForm, freq = "n", dims = c("Hair", "Eye")) +#' } +#' +#' @importFrom stats reformulate xtabs +#' @export + +as_table <- function(obj, freq = NULL, dims = NULL){ + + # If user supplied a table already, return it back to them + if (length(intersect("table", class(obj))) > 0){ + return(obj) + } + + # If obj is a tibble, convert to data frame + if (length(intersect("tbl", class(obj))) > 0){ + obj <- as.data.frame(obj) + } + + if (!is.null(dims)){ # If dims supplied by user, use those + cols <- dims + } + else { # If dims NOT supplied by user, use everything else + cols <- colnames(obj) + } + + if (!is.null(freq)){ # If freq supplied by user, then... (freq form) + cols <- cols[cols != freq] # Remove freq column + tab <- xtabs(reformulate(cols, response = freq), data = obj) # freq ~ cols + } + else { # If freq NOT supplied by user, then... (case form) + tab <- xtabs(reformulate(cols), data = obj) + + # Check if user forgot to supply freq, warn if they potentially forgot + common <- c("n", "freq", "frequency", "count") + if (length(intersect(tolower(colnames(obj)), common)) > 0){ + warning("Ensure a value for 'freq' was supplied if your data was in frequency form.") + } + } + return(tab) +} \ No newline at end of file From 9564f71228d12f7cdb53dd31c4df0217f91fb6c0 Mon Sep 17 00:00:00 2001 From: gklorfine Date: Thu, 12 Feb 2026 16:01:37 -0500 Subject: [PATCH 2/8] Added .DS_Store (a MacOS file) to .gitignore --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index cbdc114..1a5d415 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,6 @@ Rplots.pdf revdep/ /doc/ /Meta/ - # Claude Code local settings .claude/ +.DS_Store From 0c475b28fb04a00b3d7265179dc7870a1e9bc6eb Mon Sep 17 00:00:00 2001 From: gklorfine Date: Fri, 13 Feb 2026 12:32:48 -0500 Subject: [PATCH 3/8] Corrected table/array handling in as_table(). Changed documentation of as_table() and as_array(). Added as_caseform() and as_freqform() to dev/ --- dev/as_array.R | 18 +++++++++-------- dev/as_caseform.R | 45 +++++++++++++++++++++++++++++++++++++++++++ dev/as_freqform.R | 49 +++++++++++++++++++++++++++++++++++++++++++++++ dev/as_table.R | 35 +++++++++++++++++++++------------ 4 files changed, 127 insertions(+), 20 deletions(-) create mode 100644 dev/as_caseform.R create mode 100644 dev/as_freqform.R diff --git a/dev/as_array.R b/dev/as_array.R index d98012e..03978ae 100644 --- a/dev/as_array.R +++ b/dev/as_array.R @@ -1,11 +1,12 @@ -#' Convert frequency or case form data into array form +#' Convert frequency, case, or table form data into an array #' -#' Converts object (`obj`) in frequency or case form into array form. `freq` -#' must be supplied if `obj` is in frequency form. +#' Converts object (`obj`) in frequency, case or table form into an array. The +#' column containing the frequencies (`freq`) must be supplied if `obj` is in +#' frequency form. #' -#' @param obj object to be converted to array form -#' @param freq If `obj` is in frequency form, this is the name of the frequency column. Leave as `NULL` if `obj` is in case form. -#' @param dims A character vector of table dimensions. If not specified, all variables apart from `freq` will be used as dimensions +#' @param obj object to be converted to an array +#' @param freq If `obj` is in frequency form, this is the name of the frequency column. Leave as `NULL` if `obj` is in any other form. +#' @param dims A character vector of dimensions. If not specified, all variables apart from `freq` will be used as dimensions #' @return object in array form #' #' @details @@ -17,9 +18,10 @@ #' tidy_freqForm <- as_tibble(HairEyeColor) # Generate tidy frequency form data #' caseForm <- expand.dft(freqForm) # Generate case form data #' -#' as_array(freqForm, freq = "Freq") # frequency -> array form +#' as_array(freqForm, freq = "Freq") # frequency form -> array #' as_array(freqForm) # Warned if forgot freq -#' as_array(caseForm) # case form -> array form +#' as_array(caseForm) # case form -> array +#' as_table(tidy_freqForm, freq = "n") # frequency (tibble) form -> array #' #' # For specific dimensions #' as_array(tidy_freqForm, freq = "n", dims = c("Hair", "Eye")) diff --git a/dev/as_caseform.R b/dev/as_caseform.R new file mode 100644 index 0000000..9824e8f --- /dev/null +++ b/dev/as_caseform.R @@ -0,0 +1,45 @@ +#' Convert frequency or table form into case form. +#' +#' Converts object (`obj`) in frequency or table form into case form. The +#' column containing the frequencies (`freq`) must be supplied if `obj` is in +#' frequency form. Returns a tibble if `tidy` is set to `TRUE`. +#' +#' @param obj object to be converted to case form +#' @param freq If `obj` is in frequency form, this is the name of the frequency column. If `obj` is in any other form, do not supply an argument (see "Details") +#' @param dims A character vector of dimensions. If not specified, all variables apart from `freq` will be used as dimensions +#' @param tidy returns a tibble if set to TRUE +#' @return object in case form. +#' +#' @details +#' A wrapper for \code{expand.dft()} that is able to handle arrays. +#' +#' If a frequency column is not supplied, this function defaults to "Freq" +#' just like \code{expand.dft()}. Converts `obj` to a table using +#' \code{as_table()} before converting to case form. +#' +#' @examples +#' \dontrun{ +#' data("HairEyeColor") +#' freqForm <- as.data.frame(HairEyeColor) # Generate frequency form data +#' tidy_freqForm <- as_tibble(HairEyeColor) # Generate tidy frequency form data +#' tableForm <- as_table(HairEyeColor) # Generate table form data +#' arrayDat <- as_array(HairEyeColor) # Generate an array +#' +#' as_caseform(freqForm) # frequency -> case form +#' as_caseform(tidy_freqForm, freq = "n") # frequency form (tibble) -> case form +#' as_caseform(tableForm, dims = c("Hair", "Eye")) # Optionally specify dims +#' as_caseform(arrayDat) # array -> case form +#' } +#' +#' @export + +as_caseform <- function(obj, freq = "Freq", dims = NULL, tidy = TRUE){ + + tab <- expand.dft(as_table(obj, freq = freq, dims = dims), freq = freq) + + if (tidy){ + tab <- as_tibble(tab) + } + + return(tab) +} \ No newline at end of file diff --git a/dev/as_freqform.R b/dev/as_freqform.R new file mode 100644 index 0000000..a34dcc7 --- /dev/null +++ b/dev/as_freqform.R @@ -0,0 +1,49 @@ +#' Convert any form (case or table form) into frequency form. +#' +#' A wrapper for \code{as.data.frame()} that is able to properly handle arrays. +#' Converts object (`obj`) in case or table form into frequency form. The +#' column containing the frequencies (`freq`) must be supplied if `obj` is +#' already in frequency form (and you are using this function to select +#' dimensions). Returns a tibble if `tidy` is set to `TRUE`. +#' +#' @param obj object to be converted to frequency form +#' @param freq If `obj` is already in frequency form, this is the name of the frequency column. If `obj` is in any other form, do not supply an argument (see "Details") +#' @param dims A character vector of dimensions. If not specified, all variables apart from `freq` will be used as dimensions +#' @param tidy returns a tibble if set to TRUE +#' @return object in frequency form. +#' +#' @details +#' Converts `obj` to a table using \code{as_table()} before converting to +#' frequency form. +#' +#' @examples +#' \dontrun{ +#' data("HairEyeColor") +#' freqForm <- as.data.frame(HairEyeColor) # Generate frequency form data +#' tableForm <- as_table(HairEyeColor) # Generate table form data +#' arrayDat <- as_array(HairEyeColor) # Generate an array +#' caseForm <- as_caseform(HairEyeColor) # Generate case form data +#' +#' as_freqform(arrayDat) # array -> frequency form +#' as_freqform(tableForm) # table -> frequency form +#' as_freqform(caseForm) # case -> frequency form +#' +#' # Selecting dimensions (optional) +#' as_freqform(freqForm, freq = "Freq", dims = c("Hair", "Eye")) +#' as_freqform(tableForm, dims = c("Hair", "Eye")) +#' } +#' +#' @importFrom dplyr as_tibble +#' +#' @export + +as_freqform <- function(obj, freq = NULL, dims = NULL, tidy = TRUE){ + + tab <- as.data.frame(as_table(obj, freq = freq, dims = dims)) + + if (tidy){ + tab <- as_tibble(tab) + } + + return(tab) +} \ No newline at end of file diff --git a/dev/as_table.R b/dev/as_table.R index a043839..ccff60e 100644 --- a/dev/as_table.R +++ b/dev/as_table.R @@ -1,17 +1,18 @@ #' Convert frequency or case form data into table form #' -#' Converts object (`obj`) in frequency or case form into table form. `freq` -#' must be supplied if `obj` is in frequency form. +#' Converts object (`obj`) in frequency or case form into table form. The +#' column containing the frequencies (`freq`) must be supplied if `obj` is in +#' frequency form. #' #' @param obj object to be converted to table form -#' @param freq If `obj` is in frequency form, this is the name of the frequency column. Leave as `NULL` if `obj` is in case form. -#' @param dims A list of table dimensions. If not specified, all variables apart from `freq` will be used as dimensions +#' @param freq If `obj` is in frequency form, this is the name of the frequency column. Leave as `NULL` if `obj` is in any other form. +#' @param dims A character vector of dimensions. If not specified, all variables apart from `freq` will be used as dimensions #' @return object in table form #' #' @details -#' If `obj` was in table form to begin with, it is simply returned to the user -#' as-is. -#' +#' If `obj` was in table form to begin with, it is returned to the user as-is +#' unless dimensions were specified (in which case it returns a table with +#' entries summed over excluded dimensions). #' #' @examples #' \dontrun{ @@ -22,6 +23,7 @@ #' #' as_table(freqForm, freq = "Freq") # frequency -> table form #' as_table(freqForm) # Warned if forgot freq +#' as_table(tidy_freqForm, freq = "n") # frequency (tibble) -> table form #' as_table(caseForm) # case form -> table form #' #' # For specific dimensions @@ -33,13 +35,22 @@ as_table <- function(obj, freq = NULL, dims = NULL){ - # If user supplied a table already, return it back to them - if (length(intersect("table", class(obj))) > 0){ - return(obj) + # If user supplied a table or array, return it back to them + if (length(intersect(c("table", "array"), class(obj))) > 0){ + + tab <- as.table(obj) # Handle arrays + + # To include dimensions if specified + if (!is.null(dims)){ + tab <- margin.table(tab, margin = dims) + } + + return(tab) } + # If obj is a tibble, convert to data frame - if (length(intersect("tbl", class(obj))) > 0){ + else if (length(intersect("tbl", class(obj))) > 0){ obj <- as.data.frame(obj) } @@ -54,7 +65,7 @@ as_table <- function(obj, freq = NULL, dims = NULL){ cols <- cols[cols != freq] # Remove freq column tab <- xtabs(reformulate(cols, response = freq), data = obj) # freq ~ cols } - else { # If freq NOT supplied by user, then... (case form) + else { # If freq NOT supplied by user, and not array, then... (case form) tab <- xtabs(reformulate(cols), data = obj) # Check if user forgot to supply freq, warn if they potentially forgot From 58ddd8fce87bd79a77149ba2f39e7cf556d9ede4 Mon Sep 17 00:00:00 2001 From: gklorfine Date: Mon, 2 Mar 2026 17:15:34 -0500 Subject: [PATCH 4/8] Fix as_caseform documentation --- dev/as_caseform.R | 2 ++ 1 file changed, 2 insertions(+) diff --git a/dev/as_caseform.R b/dev/as_caseform.R index 9824e8f..6efd5b6 100644 --- a/dev/as_caseform.R +++ b/dev/as_caseform.R @@ -31,6 +31,8 @@ #' as_caseform(arrayDat) # array -> case form #' } #' +#' @importFrom dplyr as_tibble +#' #' @export as_caseform <- function(obj, freq = "Freq", dims = NULL, tidy = TRUE){ From c2ea51b44f84d7006955d27b8a61a156aa5613c9 Mon Sep 17 00:00:00 2001 From: gklorfine Date: Tue, 3 Mar 2026 17:04:10 -0500 Subject: [PATCH 5/8] Added proportion functionality to as_table(). Corrected documentation for as_array(), as_caseform(), as_freqform(), as_table() --- dev/as_array.R | 25 ++++++++---- dev/as_caseform.R | 29 ++++++++----- dev/as_freqform.R | 30 +++++++++----- dev/as_table.R | 102 ++++++++++++++++++++++++++++++++++++++-------- 4 files changed, 141 insertions(+), 45 deletions(-) diff --git a/dev/as_array.R b/dev/as_array.R index 03978ae..98e948b 100644 --- a/dev/as_array.R +++ b/dev/as_array.R @@ -13,19 +13,30 @@ #' Unclasses the \code{as_table()} function to return an object in array form. #' #' @examples -#' \dontrun{ +#' library(vcdExtra) +#' +#' data("HairEyeColor", package = "vcdExtra") +#' #' freqForm <- as.data.frame(HairEyeColor) # Generate frequency form data #' tidy_freqForm <- as_tibble(HairEyeColor) # Generate tidy frequency form data #' caseForm <- expand.dft(freqForm) # Generate case form data #' -#' as_array(freqForm, freq = "Freq") # frequency form -> array -#' as_array(freqForm) # Warned if forgot freq -#' as_array(caseForm) # case form -> array -#' as_table(tidy_freqForm, freq = "n") # frequency (tibble) form -> array +#' # Frequency form -> array form +#' as_array(freqForm, freq = "Freq") |> str() +#' +#' # Warned if forgot to specify freq +#' as_array(freqForm) |> str() +#' +#' # Case form -> array form +#' as_array(caseForm) |> str() +#' +#' # Frequency (tibble) form -> array form +#' as_table(tidy_freqForm, freq = "n") |> str() #' #' # For specific dimensions -#' as_array(tidy_freqForm, freq = "n", dims = c("Hair", "Eye")) -#' } +#' as_array(tidy_freqForm, freq = "n", dims = c("Hair", "Eye")) |> str() +#' +#' #' @export as_array <- function(obj, freq = NULL, dims = NULL){ diff --git a/dev/as_caseform.R b/dev/as_caseform.R index 6efd5b6..ca195bf 100644 --- a/dev/as_caseform.R +++ b/dev/as_caseform.R @@ -17,21 +17,30 @@ #' just like \code{expand.dft()}. Converts `obj` to a table using #' \code{as_table()} before converting to case form. #' +#' @importFrom dplyr as_tibble +#' #' @examples -#' \dontrun{ -#' data("HairEyeColor") +#' library(vcdExtra) +#' +#' data("HairEyeColor", package = "vcdExtra") +#' #' freqForm <- as.data.frame(HairEyeColor) # Generate frequency form data -#' tidy_freqForm <- as_tibble(HairEyeColor) # Generate tidy frequency form data +#' tidy_freqForm <- dplyr::as_tibble(HairEyeColor) # Generate tidy frequency form data #' tableForm <- as_table(HairEyeColor) # Generate table form data #' arrayDat <- as_array(HairEyeColor) # Generate an array #' -#' as_caseform(freqForm) # frequency -> case form -#' as_caseform(tidy_freqForm, freq = "n") # frequency form (tibble) -> case form -#' as_caseform(tableForm, dims = c("Hair", "Eye")) # Optionally specify dims -#' as_caseform(arrayDat) # array -> case form -#' } +#' # Frequency form -> case form +#' as_caseform(freqForm) |> str() #' -#' @importFrom dplyr as_tibble +#' # Frequency form (tibble) -> case form +#' as_caseform(tidy_freqForm, freq = "n") |> str() +#' +#' # Array -> case form +#' as_caseform(arrayDat) |> str() +#' +#' # Optionally specify dims +#' as_caseform(tableForm, dims = c("Hair", "Eye")) |> str() +#' #' #' @export @@ -40,7 +49,7 @@ as_caseform <- function(obj, freq = "Freq", dims = NULL, tidy = TRUE){ tab <- expand.dft(as_table(obj, freq = freq, dims = dims), freq = freq) if (tidy){ - tab <- as_tibble(tab) + tab <- dplyr::as_tibble(tab) } return(tab) diff --git a/dev/as_freqform.R b/dev/as_freqform.R index a34dcc7..d99c406 100644 --- a/dev/as_freqform.R +++ b/dev/as_freqform.R @@ -14,26 +14,34 @@ #' #' @details #' Converts `obj` to a table using \code{as_table()} before converting to -#' frequency form. +#' frequency form +#' +#' @importFrom dplyr as_tibble. #' #' @examples -#' \dontrun{ -#' data("HairEyeColor") +#' library(vcdExtra) +#' +#' data("HairEyeColor", package = "vcdExtra") +#' #' freqForm <- as.data.frame(HairEyeColor) # Generate frequency form data #' tableForm <- as_table(HairEyeColor) # Generate table form data #' arrayDat <- as_array(HairEyeColor) # Generate an array #' caseForm <- as_caseform(HairEyeColor) # Generate case form data #' -#' as_freqform(arrayDat) # array -> frequency form -#' as_freqform(tableForm) # table -> frequency form -#' as_freqform(caseForm) # case -> frequency form +#' # array -> frequency form +#' as_freqform(arrayDat) |> str() +#' +#' # table -> frequency form +#' as_freqform(tableForm) |> str() +#' +#' # case -> frequency form +#' as_freqform(caseForm) |> str() #' #' # Selecting dimensions (optional) -#' as_freqform(freqForm, freq = "Freq", dims = c("Hair", "Eye")) -#' as_freqform(tableForm, dims = c("Hair", "Eye")) -#' } +#' as_freqform(freqForm, freq = "Freq", dims = c("Hair", "Eye")) |> str() +#' +#' as_freqform(tableForm, dims = c("Hair", "Eye")) |> str() #' -#' @importFrom dplyr as_tibble #' #' @export @@ -42,7 +50,7 @@ as_freqform <- function(obj, freq = NULL, dims = NULL, tidy = TRUE){ tab <- as.data.frame(as_table(obj, freq = freq, dims = dims)) if (tidy){ - tab <- as_tibble(tab) + tab <- dplyr::as_tibble(tab) } return(tab) diff --git a/dev/as_table.R b/dev/as_table.R index ccff60e..ec1e64c 100644 --- a/dev/as_table.R +++ b/dev/as_table.R @@ -2,40 +2,71 @@ #' #' Converts object (`obj`) in frequency or case form into table form. The #' column containing the frequencies (`freq`) must be supplied if `obj` is in -#' frequency form. +#' frequency form. Optionally returns a table of proportions with (optionally) specified margins. #' #' @param obj object to be converted to table form #' @param freq If `obj` is in frequency form, this is the name of the frequency column. Leave as `NULL` if `obj` is in any other form. #' @param dims A character vector of dimensions. If not specified, all variables apart from `freq` will be used as dimensions +#' @param prop If set to TRUE, returns a table of proportions. May also be set to a character or numeric vector of margins. #' @return object in table form #' #' @details #' If `obj` was in table form to begin with, it is returned to the user as-is #' unless dimensions were specified (in which case it returns a table with -#' entries summed over excluded dimensions). +#' entries summed over excluded dimensions). When `prop` is set to `TRUE`, the +#' returned table will have proportions that sum to one, whereas if a character +#' or numerical vector of table dimensions is supplied to `prop`, proportions +#' will be marginalized across the specified dimensions. +#' +#' @importFrom stats reformulate xtabs #' #' @examples -#' \dontrun{ -#' data("HairEyeColor") +#' library(vcdExtra) +#' +#' data("HairEyeColor", package = "vcdExtra") +#' #' freqForm <- as.data.frame(HairEyeColor) # Generate frequency form data #' tidy_freqForm <- as_tibble(HairEyeColor) # Generate tidy frequency form data #' caseForm <- expand.dft(freqForm) # Generate case form data #' -#' as_table(freqForm, freq = "Freq") # frequency -> table form -#' as_table(freqForm) # Warned if forgot freq -#' as_table(tidy_freqForm, freq = "n") # frequency (tibble) -> table form -#' as_table(caseForm) # case form -> table form +#' # Frequency form -> table form +#' as_table(freqForm, freq = "Freq") |> str() +#' +#' # Warned if forgot to specify freq +#' as_table(freqForm) |> str() +#' +#' # Frequency form (tibble) -> table form +#' as_table(tidy_freqForm, freq = "n") |> str() +#' +#' # Case form -> table form +#' as_table(caseForm) |> str() #' #' # For specific dimensions -#' as_table(tidy_freqForm, freq = "n", dims = c("Hair", "Eye")) -#' } +#' as_table(tidy_freqForm, freq = "n", dims = c("Hair", "Eye")) |> str() +#' +#' #-----For proportions-----# +#' +#' as_table(freqForm, freq = "Freq", prop = TRUE) |> head(c(4,4,1)) # print only Sex == Male rows +#' +#' # Marginalize proportions along "Sex" (i.e., male proportions sum to 1, female proportions sum to 1) +#' as_table(freqForm, freq = "Freq", prop = "Sex") |> head(c(4,4,1)) +#' +#' as_table(freqForm, freq = "Freq", prop = 3) |> head(c(4,4,1)) # Same as above +#' +#' # Marginalize proportions along multiple variables +#' as_table(freqForm, freq = "Freq", prop = c("Hair", "Sex")) |> head(c(4,4,1)) +#' +#' as_table(freqForm, freq = "Freq", prop = c(1, 3)) |> head(c(4,4,1)) # Same as above +#' +#' # Using dims and prop arguments in tandem +#' as_table(freqForm, freq = "Freq", dims = c("Hair", "Eye"), prop = TRUE) +#' #' -#' @importFrom stats reformulate xtabs #' @export -as_table <- function(obj, freq = NULL, dims = NULL){ +as_table <- function(obj, freq = NULL, dims = NULL, prop = NULL){ - # If user supplied a table or array, return it back to them + # If user supplied a table or array, remember that if (length(intersect(c("table", "array"), class(obj))) > 0){ tab <- as.table(obj) # Handle arrays @@ -45,10 +76,8 @@ as_table <- function(obj, freq = NULL, dims = NULL){ tab <- margin.table(tab, margin = dims) } - return(tab) + tab_or_array <- TRUE } - - # If obj is a tibble, convert to data frame else if (length(intersect("tbl", class(obj))) > 0){ obj <- as.data.frame(obj) @@ -65,7 +94,7 @@ as_table <- function(obj, freq = NULL, dims = NULL){ cols <- cols[cols != freq] # Remove freq column tab <- xtabs(reformulate(cols, response = freq), data = obj) # freq ~ cols } - else { # If freq NOT supplied by user, and not array, then... (case form) + else if (is.null(freq) && !tab_or_array){ # If freq NOT supplied by user, and not array, then... (case form) tab <- xtabs(reformulate(cols), data = obj) # Check if user forgot to supply freq, warn if they potentially forgot @@ -74,5 +103,44 @@ as_table <- function(obj, freq = NULL, dims = NULL){ warning("Ensure a value for 'freq' was supplied if your data was in frequency form.") } } + + + if (!is.null(prop)){ # If user wants proportions + + if (class(prop) == "logical" && prop == TRUE){ # If margins not specified + tab <- prop.table(tab) + } + else if (is.character(prop) || is.numeric(prop)){ # If proportions are to be marginal + + ### Make sure margins are not problematic ### + if (length(prop) > length(dim(tab))){ # Raise error if # of margin dims exceeds actual # of dims + stop("Number of specified margins in `prop` exceeds number of dims in table.") + } + if (length(unique(prop)) != length(prop)){ # Make sure prop margins are unique + stop("`prop` margins are not unique (i.e., a duplicate was provided).") + } + # If character vector for proportion margins, make sure margins in prop are a subset of dims in table + if (is.character(prop) && length(prop) != length(intersect(prop, names(dimnames(tab))))){ + stop("Ensure all margins specified in `prop` are dims in the table.") + } + # If numeric vector for proportion margins, + else if(is.numeric(prop)){ + if (!all(prop > 0) || !all(prop %% 1 == 0)){ # Make sure margins in prop are positive, whole nums + stop("Ensure all margins specified in `prop` are positive whole numbers.") + } + if (!all(prop <= length(dim(tab)))){ # Make sure margins in prop do not exceed # of dims in table + stop("Ensure all margins specified in `prop` do not exceed the total number of dims in the table.") + } + } + ### + + tab <- prop.table(tab, margin = prop) + } + else{ # If prop not TRUE or a numeric / character vector + stop("Argument `prop` must be supplied with either a numeric or character vector") + } + } + + return(tab) } \ No newline at end of file From aff79a423c9f7b8262d70b2c310fd50f1a590196 Mon Sep 17 00:00:00 2001 From: gklorfine Date: Wed, 4 Mar 2026 12:06:10 -0500 Subject: [PATCH 6/8] Moved as_array(), as_caseform(), as_freqform(), as_table() from dev/ to R/. Documented functions via devtools::document(). Added `methods` to imports in DESCRIPTION. --- DESCRIPTION | 3 +- NAMESPACE | 6 +++ {dev => R}/as_array.R | 6 ++- {dev => R}/as_caseform.R | 2 + {dev => R}/as_freqform.R | 4 +- {dev => R}/as_table.R | 36 ++++++++++-------- man/as_array.Rd | 55 ++++++++++++++++++++++++++++ man/as_caseform.Rd | 59 ++++++++++++++++++++++++++++++ man/as_freqform.Rd | 60 ++++++++++++++++++++++++++++++ man/as_table.Rd | 79 ++++++++++++++++++++++++++++++++++++++++ 10 files changed, 291 insertions(+), 19 deletions(-) rename {dev => R}/as_array.R (89%) rename {dev => R}/as_caseform.R (98%) rename {dev => R}/as_freqform.R (96%) rename {dev => R}/as_table.R (82%) create mode 100644 man/as_array.Rd create mode 100644 man/as_caseform.Rd create mode 100644 man/as_freqform.Rd create mode 100644 man/as_table.Rd diff --git a/DESCRIPTION b/DESCRIPTION index f23cec9..82fafac 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -74,7 +74,8 @@ Imports: rgl, colorspace, gt, - scales + scales, + methods Description: Provides additional data sets, methods and documentation to complement the 'vcd' package for Visualizing Categorical Data and the 'gnm' package for Generalized Nonlinear Models. In particular, 'vcdExtra' extends mosaic, assoc and sieve plots from 'vcd' to handle 'glm()' and 'gnm()' models and diff --git a/NAMESPACE b/NAMESPACE index d98bc0c..32857be 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -48,6 +48,10 @@ export(HLtest) export(Kway) export(LRstats) export(Summarise) +export(as_array) +export(as_caseform) +export(as_freqform) +export(as_table) export(assoc_graph) export(blogits) export(center3d) @@ -87,6 +91,7 @@ importFrom(MASS,loglm) importFrom(ca,cacoord) importFrom(ca,multilines) importFrom(dplyr,all_of) +importFrom(dplyr,as_tibble) importFrom(dplyr,everything) importFrom(gnm,meanResiduals) importFrom(grDevices,col2rgb) @@ -106,6 +111,7 @@ importFrom(grid,seekViewport) importFrom(grid,unit) importFrom(grid,upViewport) importFrom(grid,viewport) +importFrom(methods,is) importFrom(rgl,translate3d) importFrom(stats,as.formula) importFrom(stats,chisq.test) diff --git a/dev/as_array.R b/R/as_array.R similarity index 89% rename from dev/as_array.R rename to R/as_array.R index 98e948b..9267d7c 100644 --- a/dev/as_array.R +++ b/R/as_array.R @@ -12,13 +12,15 @@ #' @details #' Unclasses the \code{as_table()} function to return an object in array form. #' +#' @author Gavin M. Klorfine +#' #' @examples #' library(vcdExtra) #' #' data("HairEyeColor", package = "vcdExtra") #' #' freqForm <- as.data.frame(HairEyeColor) # Generate frequency form data -#' tidy_freqForm <- as_tibble(HairEyeColor) # Generate tidy frequency form data +#' tidy_freqForm <- dplyr::as_tibble(HairEyeColor) # Generate tidy frequency form data #' caseForm <- expand.dft(freqForm) # Generate case form data #' #' # Frequency form -> array form @@ -31,7 +33,7 @@ #' as_array(caseForm) |> str() #' #' # Frequency (tibble) form -> array form -#' as_table(tidy_freqForm, freq = "n") |> str() +#' as_array(tidy_freqForm, freq = "n") |> str() #' #' # For specific dimensions #' as_array(tidy_freqForm, freq = "n", dims = c("Hair", "Eye")) |> str() diff --git a/dev/as_caseform.R b/R/as_caseform.R similarity index 98% rename from dev/as_caseform.R rename to R/as_caseform.R index ca195bf..7e6da8f 100644 --- a/dev/as_caseform.R +++ b/R/as_caseform.R @@ -17,6 +17,8 @@ #' just like \code{expand.dft()}. Converts `obj` to a table using #' \code{as_table()} before converting to case form. #' +#' @author Gavin M. Klorfine +#' #' @importFrom dplyr as_tibble #' #' @examples diff --git a/dev/as_freqform.R b/R/as_freqform.R similarity index 96% rename from dev/as_freqform.R rename to R/as_freqform.R index d99c406..6e0a1cf 100644 --- a/dev/as_freqform.R +++ b/R/as_freqform.R @@ -16,7 +16,9 @@ #' Converts `obj` to a table using \code{as_table()} before converting to #' frequency form #' -#' @importFrom dplyr as_tibble. +#' @author Gavin M. Klorfine +#' +#' @importFrom dplyr as_tibble #' #' @examples #' library(vcdExtra) diff --git a/dev/as_table.R b/R/as_table.R similarity index 82% rename from dev/as_table.R rename to R/as_table.R index ec1e64c..53827fb 100644 --- a/dev/as_table.R +++ b/R/as_table.R @@ -18,7 +18,10 @@ #' or numerical vector of table dimensions is supplied to `prop`, proportions #' will be marginalized across the specified dimensions. #' +#' @author Gavin M. Klorfine +#' #' @importFrom stats reformulate xtabs +#' @importFrom methods is #' #' @examples #' library(vcdExtra) @@ -26,7 +29,7 @@ #' data("HairEyeColor", package = "vcdExtra") #' #' freqForm <- as.data.frame(HairEyeColor) # Generate frequency form data -#' tidy_freqForm <- as_tibble(HairEyeColor) # Generate tidy frequency form data +#' tidy_freqForm <- dplyr::as_tibble(HairEyeColor) # Generate tidy frequency form data #' caseForm <- expand.dft(freqForm) # Generate case form data #' #' # Frequency form -> table form @@ -67,7 +70,8 @@ as_table <- function(obj, freq = NULL, dims = NULL, prop = NULL){ # If user supplied a table or array, remember that - if (length(intersect(c("table", "array"), class(obj))) > 0){ + tab_or_array <- FALSE + if (is(obj, "table") || is(obj, "array")){ tab <- as.table(obj) # Handle arrays @@ -79,7 +83,7 @@ as_table <- function(obj, freq = NULL, dims = NULL, prop = NULL){ tab_or_array <- TRUE } # If obj is a tibble, convert to data frame - else if (length(intersect("tbl", class(obj))) > 0){ + else if (is(obj, "table")){ obj <- as.data.frame(obj) } @@ -90,24 +94,26 @@ as_table <- function(obj, freq = NULL, dims = NULL, prop = NULL){ cols <- colnames(obj) } - if (!is.null(freq)){ # If freq supplied by user, then... (freq form) - cols <- cols[cols != freq] # Remove freq column - tab <- xtabs(reformulate(cols, response = freq), data = obj) # freq ~ cols - } - else if (is.null(freq) && !tab_or_array){ # If freq NOT supplied by user, and not array, then... (case form) - tab <- xtabs(reformulate(cols), data = obj) - - # Check if user forgot to supply freq, warn if they potentially forgot - common <- c("n", "freq", "frequency", "count") - if (length(intersect(tolower(colnames(obj)), common)) > 0){ - warning("Ensure a value for 'freq' was supplied if your data was in frequency form.") + if (!tab_or_array){ # If not a table or array... + if (!is.null(freq)){ # If freq supplied by user, then... (freq form) + cols <- cols[cols != freq] # Remove freq column + tab <- xtabs(reformulate(cols, response = freq), data = obj) # freq ~ cols + } + else if (is.null(freq)){ # If freq NOT supplied by user, and not array, then... (case form) + tab <- xtabs(reformulate(cols), data = obj) + + # Check if user forgot to supply freq, warn if they potentially forgot + common <- c("n", "freq", "frequency", "count") + if (length(intersect(tolower(colnames(obj)), common)) > 0){ + warning("Ensure a value for 'freq' was supplied if your data was in frequency form.") + } } } if (!is.null(prop)){ # If user wants proportions - if (class(prop) == "logical" && prop == TRUE){ # If margins not specified + if (is(prop, "logical") && prop == TRUE){ # If margins not specified tab <- prop.table(tab) } else if (is.character(prop) || is.numeric(prop)){ # If proportions are to be marginal diff --git a/man/as_array.Rd b/man/as_array.Rd new file mode 100644 index 0000000..2ff837c --- /dev/null +++ b/man/as_array.Rd @@ -0,0 +1,55 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/as_array.R +\name{as_array} +\alias{as_array} +\title{Convert frequency, case, or table form data into an array} +\usage{ +as_array(obj, freq = NULL, dims = NULL) +} +\arguments{ +\item{obj}{object to be converted to an array} + +\item{freq}{If \code{obj} is in frequency form, this is the name of the frequency column. Leave as \code{NULL} if \code{obj} is in any other form.} + +\item{dims}{A character vector of dimensions. If not specified, all variables apart from \code{freq} will be used as dimensions} +} +\value{ +object in array form +} +\description{ +Converts object (\code{obj}) in frequency, case or table form into an array. The +column containing the frequencies (\code{freq}) must be supplied if \code{obj} is in +frequency form. +} +\details{ +Unclasses the \code{as_table()} function to return an object in array form. +} +\examples{ +library(vcdExtra) + +data("HairEyeColor", package = "vcdExtra") + +freqForm <- as.data.frame(HairEyeColor) # Generate frequency form data +tidy_freqForm <- dplyr::as_tibble(HairEyeColor) # Generate tidy frequency form data +caseForm <- expand.dft(freqForm) # Generate case form data + +# Frequency form -> array form +as_array(freqForm, freq = "Freq") |> str() + +# Warned if forgot to specify freq +as_array(freqForm) |> str() + +# Case form -> array form +as_array(caseForm) |> str() + +# Frequency (tibble) form -> array form +as_array(tidy_freqForm, freq = "n") |> str() + +# For specific dimensions +as_array(tidy_freqForm, freq = "n", dims = c("Hair", "Eye")) |> str() + + +} +\author{ +Gavin M. Klorfine +} diff --git a/man/as_caseform.Rd b/man/as_caseform.Rd new file mode 100644 index 0000000..5ecb265 --- /dev/null +++ b/man/as_caseform.Rd @@ -0,0 +1,59 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/as_caseform.R +\name{as_caseform} +\alias{as_caseform} +\title{Convert frequency or table form into case form.} +\usage{ +as_caseform(obj, freq = "Freq", dims = NULL, tidy = TRUE) +} +\arguments{ +\item{obj}{object to be converted to case form} + +\item{freq}{If \code{obj} is in frequency form, this is the name of the frequency column. If \code{obj} is in any other form, do not supply an argument (see "Details")} + +\item{dims}{A character vector of dimensions. If not specified, all variables apart from \code{freq} will be used as dimensions} + +\item{tidy}{returns a tibble if set to TRUE} +} +\value{ +object in case form. +} +\description{ +Converts object (\code{obj}) in frequency or table form into case form. The +column containing the frequencies (\code{freq}) must be supplied if \code{obj} is in +frequency form. Returns a tibble if \code{tidy} is set to \code{TRUE}. +} +\details{ +A wrapper for \code{expand.dft()} that is able to handle arrays. + +If a frequency column is not supplied, this function defaults to "Freq" +just like \code{expand.dft()}. Converts \code{obj} to a table using +\code{as_table()} before converting to case form. +} +\examples{ +library(vcdExtra) + +data("HairEyeColor", package = "vcdExtra") + +freqForm <- as.data.frame(HairEyeColor) # Generate frequency form data +tidy_freqForm <- dplyr::as_tibble(HairEyeColor) # Generate tidy frequency form data +tableForm <- as_table(HairEyeColor) # Generate table form data +arrayDat <- as_array(HairEyeColor) # Generate an array + +# Frequency form -> case form +as_caseform(freqForm) |> str() + +# Frequency form (tibble) -> case form +as_caseform(tidy_freqForm, freq = "n") |> str() + +# Array -> case form +as_caseform(arrayDat) |> str() + +# Optionally specify dims +as_caseform(tableForm, dims = c("Hair", "Eye")) |> str() + + +} +\author{ +Gavin M. Klorfine +} diff --git a/man/as_freqform.Rd b/man/as_freqform.Rd new file mode 100644 index 0000000..3460c28 --- /dev/null +++ b/man/as_freqform.Rd @@ -0,0 +1,60 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/as_freqform.R +\name{as_freqform} +\alias{as_freqform} +\title{Convert any form (case or table form) into frequency form.} +\usage{ +as_freqform(obj, freq = NULL, dims = NULL, tidy = TRUE) +} +\arguments{ +\item{obj}{object to be converted to frequency form} + +\item{freq}{If \code{obj} is already in frequency form, this is the name of the frequency column. If \code{obj} is in any other form, do not supply an argument (see "Details")} + +\item{dims}{A character vector of dimensions. If not specified, all variables apart from \code{freq} will be used as dimensions} + +\item{tidy}{returns a tibble if set to TRUE} +} +\value{ +object in frequency form. +} +\description{ +A wrapper for \code{as.data.frame()} that is able to properly handle arrays. +Converts object (\code{obj}) in case or table form into frequency form. The +column containing the frequencies (\code{freq}) must be supplied if \code{obj} is +already in frequency form (and you are using this function to select +dimensions). Returns a tibble if \code{tidy} is set to \code{TRUE}. +} +\details{ +Converts \code{obj} to a table using \code{as_table()} before converting to +frequency form +} +\examples{ +library(vcdExtra) + +data("HairEyeColor", package = "vcdExtra") + +freqForm <- as.data.frame(HairEyeColor) # Generate frequency form data +tableForm <- as_table(HairEyeColor) # Generate table form data +arrayDat <- as_array(HairEyeColor) # Generate an array +caseForm <- as_caseform(HairEyeColor) # Generate case form data + +# array -> frequency form +as_freqform(arrayDat) |> str() + +# table -> frequency form +as_freqform(tableForm) |> str() + +# case -> frequency form +as_freqform(caseForm) |> str() + +# Selecting dimensions (optional) +as_freqform(freqForm, freq = "Freq", dims = c("Hair", "Eye")) |> str() + +as_freqform(tableForm, dims = c("Hair", "Eye")) |> str() + + +} +\author{ +Gavin M. Klorfine +} diff --git a/man/as_table.Rd b/man/as_table.Rd new file mode 100644 index 0000000..378aa5d --- /dev/null +++ b/man/as_table.Rd @@ -0,0 +1,79 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/as_table.R +\name{as_table} +\alias{as_table} +\title{Convert frequency or case form data into table form} +\usage{ +as_table(obj, freq = NULL, dims = NULL, prop = NULL) +} +\arguments{ +\item{obj}{object to be converted to table form} + +\item{freq}{If \code{obj} is in frequency form, this is the name of the frequency column. Leave as \code{NULL} if \code{obj} is in any other form.} + +\item{dims}{A character vector of dimensions. If not specified, all variables apart from \code{freq} will be used as dimensions} + +\item{prop}{If set to TRUE, returns a table of proportions. May also be set to a character or numeric vector of margins.} +} +\value{ +object in table form +} +\description{ +Converts object (\code{obj}) in frequency or case form into table form. The +column containing the frequencies (\code{freq}) must be supplied if \code{obj} is in +frequency form. Optionally returns a table of proportions with (optionally) specified margins. +} +\details{ +If \code{obj} was in table form to begin with, it is returned to the user as-is +unless dimensions were specified (in which case it returns a table with +entries summed over excluded dimensions). When \code{prop} is set to \code{TRUE}, the +returned table will have proportions that sum to one, whereas if a character +or numerical vector of table dimensions is supplied to \code{prop}, proportions +will be marginalized across the specified dimensions. +} +\examples{ +library(vcdExtra) + +data("HairEyeColor", package = "vcdExtra") + +freqForm <- as.data.frame(HairEyeColor) # Generate frequency form data +tidy_freqForm <- dplyr::as_tibble(HairEyeColor) # Generate tidy frequency form data +caseForm <- expand.dft(freqForm) # Generate case form data + +# Frequency form -> table form +as_table(freqForm, freq = "Freq") |> str() + +# Warned if forgot to specify freq +as_table(freqForm) |> str() + +# Frequency form (tibble) -> table form +as_table(tidy_freqForm, freq = "n") |> str() + +# Case form -> table form +as_table(caseForm) |> str() + +# For specific dimensions +as_table(tidy_freqForm, freq = "n", dims = c("Hair", "Eye")) |> str() + +#-----For proportions-----# + +as_table(freqForm, freq = "Freq", prop = TRUE) |> head(c(4,4,1)) # print only Sex == Male rows + +# Marginalize proportions along "Sex" (i.e., male proportions sum to 1, female proportions sum to 1) +as_table(freqForm, freq = "Freq", prop = "Sex") |> head(c(4,4,1)) + +as_table(freqForm, freq = "Freq", prop = 3) |> head(c(4,4,1)) # Same as above + +# Marginalize proportions along multiple variables +as_table(freqForm, freq = "Freq", prop = c("Hair", "Sex")) |> head(c(4,4,1)) + +as_table(freqForm, freq = "Freq", prop = c(1, 3)) |> head(c(4,4,1)) # Same as above + +# Using dims and prop arguments in tandem +as_table(freqForm, freq = "Freq", dims = c("Hair", "Eye"), prop = TRUE) + + +} +\author{ +Gavin M. Klorfine +} From 553f0f556a9cf83cb07886f481922aff479acadb Mon Sep 17 00:00:00 2001 From: gklorfine Date: Thu, 5 Mar 2026 14:56:25 -0500 Subject: [PATCH 7/8] Added tests for as_table(). Modified documentation for as_* functions. --- R/as_array.R | 2 +- R/as_caseform.R | 2 +- R/as_freqform.R | 2 +- R/as_table.R | 2 +- man/as_array.Rd | 2 +- man/as_caseform.Rd | 2 +- man/as_freqform.Rd | 2 +- man/as_table.Rd | 2 +- tests/testthat/test-as_table.R | 47 ++++++++++++++++++++++++++++++++++ 9 files changed, 55 insertions(+), 8 deletions(-) create mode 100644 tests/testthat/test-as_table.R diff --git a/R/as_array.R b/R/as_array.R index 9267d7c..09481c1 100644 --- a/R/as_array.R +++ b/R/as_array.R @@ -17,7 +17,7 @@ #' @examples #' library(vcdExtra) #' -#' data("HairEyeColor", package = "vcdExtra") +#' data("HairEyeColor") #' #' freqForm <- as.data.frame(HairEyeColor) # Generate frequency form data #' tidy_freqForm <- dplyr::as_tibble(HairEyeColor) # Generate tidy frequency form data diff --git a/R/as_caseform.R b/R/as_caseform.R index 7e6da8f..a9344a2 100644 --- a/R/as_caseform.R +++ b/R/as_caseform.R @@ -24,7 +24,7 @@ #' @examples #' library(vcdExtra) #' -#' data("HairEyeColor", package = "vcdExtra") +#' data("HairEyeColor") #' #' freqForm <- as.data.frame(HairEyeColor) # Generate frequency form data #' tidy_freqForm <- dplyr::as_tibble(HairEyeColor) # Generate tidy frequency form data diff --git a/R/as_freqform.R b/R/as_freqform.R index 6e0a1cf..a944961 100644 --- a/R/as_freqform.R +++ b/R/as_freqform.R @@ -23,7 +23,7 @@ #' @examples #' library(vcdExtra) #' -#' data("HairEyeColor", package = "vcdExtra") +#' data("HairEyeColor") #' #' freqForm <- as.data.frame(HairEyeColor) # Generate frequency form data #' tableForm <- as_table(HairEyeColor) # Generate table form data diff --git a/R/as_table.R b/R/as_table.R index 53827fb..369adaf 100644 --- a/R/as_table.R +++ b/R/as_table.R @@ -26,7 +26,7 @@ #' @examples #' library(vcdExtra) #' -#' data("HairEyeColor", package = "vcdExtra") +#' data("HairEyeColor") #' #' freqForm <- as.data.frame(HairEyeColor) # Generate frequency form data #' tidy_freqForm <- dplyr::as_tibble(HairEyeColor) # Generate tidy frequency form data diff --git a/man/as_array.Rd b/man/as_array.Rd index 2ff837c..49c8814 100644 --- a/man/as_array.Rd +++ b/man/as_array.Rd @@ -27,7 +27,7 @@ Unclasses the \code{as_table()} function to return an object in array form. \examples{ library(vcdExtra) -data("HairEyeColor", package = "vcdExtra") +data("HairEyeColor") freqForm <- as.data.frame(HairEyeColor) # Generate frequency form data tidy_freqForm <- dplyr::as_tibble(HairEyeColor) # Generate tidy frequency form data diff --git a/man/as_caseform.Rd b/man/as_caseform.Rd index 5ecb265..b82e879 100644 --- a/man/as_caseform.Rd +++ b/man/as_caseform.Rd @@ -33,7 +33,7 @@ just like \code{expand.dft()}. Converts \code{obj} to a table using \examples{ library(vcdExtra) -data("HairEyeColor", package = "vcdExtra") +data("HairEyeColor") freqForm <- as.data.frame(HairEyeColor) # Generate frequency form data tidy_freqForm <- dplyr::as_tibble(HairEyeColor) # Generate tidy frequency form data diff --git a/man/as_freqform.Rd b/man/as_freqform.Rd index 3460c28..5528bab 100644 --- a/man/as_freqform.Rd +++ b/man/as_freqform.Rd @@ -32,7 +32,7 @@ frequency form \examples{ library(vcdExtra) -data("HairEyeColor", package = "vcdExtra") +data("HairEyeColor") freqForm <- as.data.frame(HairEyeColor) # Generate frequency form data tableForm <- as_table(HairEyeColor) # Generate table form data diff --git a/man/as_table.Rd b/man/as_table.Rd index 378aa5d..ec09271 100644 --- a/man/as_table.Rd +++ b/man/as_table.Rd @@ -34,7 +34,7 @@ will be marginalized across the specified dimensions. \examples{ library(vcdExtra) -data("HairEyeColor", package = "vcdExtra") +data("HairEyeColor") freqForm <- as.data.frame(HairEyeColor) # Generate frequency form data tidy_freqForm <- dplyr::as_tibble(HairEyeColor) # Generate tidy frequency form data diff --git a/tests/testthat/test-as_table.R b/tests/testthat/test-as_table.R new file mode 100644 index 0000000..78a92aa --- /dev/null +++ b/tests/testthat/test-as_table.R @@ -0,0 +1,47 @@ +test_that("as_table returns table if table is supplied", { + data("HairEyeColor") + tab <- as_table(HairEyeColor) + expect_s3_class(tab, "table") +}) + +test_that("as_table returns table if array is supplied", { + data("HairEyeColor") + tab <- as_table(unclass(HairEyeColor)) # unclass() converts table -> array + expect_s3_class(tab, "table") +}) + +test_that("as_table returns table if freqform is supplied", { + data("HairEyeColor") + tab <- as_table(as.data.frame(HairEyeColor), freq = "Freq") + expect_s3_class(tab, "table") +}) + +test_that("as_table returns table if case form is supplied", { + data("HairEyeColor") + tab <- as_table(expand.dft(HairEyeColor)) + expect_s3_class(tab, "table") +}) + +test_that("as_table does not modify table entries", { + data("HairEyeColor") + tab <- as_table(HairEyeColor) + tab_freq <- as_table(as.data.frame(HairEyeColor), freq = "Freq") + tab_case <- as_table(expand.dft(HairEyeColor)) + expect_equal(sum(tab), sum(HairEyeColor)) + expect_equal(sum(tab_freq), sum(tab_case)) + expect_equal(sum(tab), sum(tab_case)) +}) + +test_that("Proportions sum to one", { + data("HairEyeColor") + tab <- as_table(HairEyeColor, prop = TRUE) + expect_equal(sum(tab), 1) +}) + +test_that("Marginal proportions sum to one", { + data("HairEyeColor") + tab <- as_table(HairEyeColor, prop = c("Hair", "Sex")) + expect_equal(sum(tab["Black",,"Male"]), 1) + tab <- as_table(HairEyeColor, prop = "Hair") + expect_equal(sum(tab["Black",,]), 1) +}) From 5de23dc3cab06a3860b5e208bf732588e077e791 Mon Sep 17 00:00:00 2001 From: gklorfine Date: Thu, 5 Mar 2026 15:09:50 -0500 Subject: [PATCH 8/8] Added as_* conversion functions to _pkgdown.yml --- _pkgdown.yml | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/_pkgdown.yml b/_pkgdown.yml index 801b496..3cdd9a1 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -141,4 +141,13 @@ reference: - Vietnam - Vote1980 - WorkerSat - - Yamaguchi87 + - Yamaguchi87 + + - title: Conversions + desc: converting between table, freq, case, array forms + contents: + - as_table + - as_array + - as_freqform + - as_caseform +