Skip to content

Commit bfc1d95

Browse files
committed
Added as_table() and as_array() to dev/
1 parent b75f0fe commit bfc1d95

2 files changed

Lines changed: 98 additions & 0 deletions

File tree

dev/as_array.R

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#' Convert frequency or case form data into array form
2+
#'
3+
#' Converts object (`obj`) in frequency or case form into array form. `freq`
4+
#' must be supplied if `obj` is in frequency form.
5+
#'
6+
#' @param obj object to be converted to array form
7+
#' @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.
8+
#' @param dims A character vector of table dimensions. If not specified, all variables apart from `freq` will be used as dimensions
9+
#' @return object in array form
10+
#'
11+
#' @details
12+
#' Unclasses the \code{as_table()} function to return an object in array form.
13+
#'
14+
#' @examples
15+
#' \dontrun{
16+
#' freqForm <- as.data.frame(HairEyeColor) # Generate frequency form data
17+
#' tidy_freqForm <- as_tibble(HairEyeColor) # Generate tidy frequency form data
18+
#' caseForm <- expand.dft(freqForm) # Generate case form data
19+
#'
20+
#' as_array(freqForm, freq = "Freq") # frequency -> array form
21+
#' as_array(freqForm) # Warned if forgot freq
22+
#' as_array(caseForm) # case form -> array form
23+
#'
24+
#' # For specific dimensions
25+
#' as_array(tidy_freqForm, freq = "n", dims = c("Hair", "Eye"))
26+
#' }
27+
#' @export
28+
29+
as_array <- function(obj, freq = NULL, dims = NULL){
30+
return(unclass(as_table(obj, freq, dims))) # Unclass as_table output
31+
}

dev/as_table.R

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#' Convert frequency or case form data into table form
2+
#'
3+
#' Converts object (`obj`) in frequency or case form into table form. `freq`
4+
#' must be supplied if `obj` is in frequency form.
5+
#'
6+
#' @param obj object to be converted to table form
7+
#' @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.
8+
#' @param dims A list of table dimensions. If not specified, all variables apart from `freq` will be used as dimensions
9+
#' @return object in table form
10+
#'
11+
#' @details
12+
#' If `obj` was in table form to begin with, it is simply returned to the user
13+
#' as-is.
14+
#'
15+
#'
16+
#' @examples
17+
#' \dontrun{
18+
#' data("HairEyeColor")
19+
#' freqForm <- as.data.frame(HairEyeColor) # Generate frequency form data
20+
#' tidy_freqForm <- as_tibble(HairEyeColor) # Generate tidy frequency form data
21+
#' caseForm <- expand.dft(freqForm) # Generate case form data
22+
#'
23+
#' as_table(freqForm, freq = "Freq") # frequency -> table form
24+
#' as_table(freqForm) # Warned if forgot freq
25+
#' as_table(caseForm) # case form -> table form
26+
#'
27+
#' # For specific dimensions
28+
#' as_table(tidy_freqForm, freq = "n", dims = c("Hair", "Eye"))
29+
#' }
30+
#'
31+
#' @importFrom stats reformulate xtabs
32+
#' @export
33+
34+
as_table <- function(obj, freq = NULL, dims = NULL){
35+
36+
# If user supplied a table already, return it back to them
37+
if (length(intersect("table", class(obj))) > 0){
38+
return(obj)
39+
}
40+
41+
# If obj is a tibble, convert to data frame
42+
if (length(intersect("tbl", class(obj))) > 0){
43+
obj <- as.data.frame(obj)
44+
}
45+
46+
if (!is.null(dims)){ # If dims supplied by user, use those
47+
cols <- dims
48+
}
49+
else { # If dims NOT supplied by user, use everything else
50+
cols <- colnames(obj)
51+
}
52+
53+
if (!is.null(freq)){ # If freq supplied by user, then... (freq form)
54+
cols <- cols[cols != freq] # Remove freq column
55+
tab <- xtabs(reformulate(cols, response = freq), data = obj) # freq ~ cols
56+
}
57+
else { # If freq NOT supplied by user, then... (case form)
58+
tab <- xtabs(reformulate(cols), data = obj)
59+
60+
# Check if user forgot to supply freq, warn if they potentially forgot
61+
common <- c("n", "freq", "frequency", "count")
62+
if (length(intersect(tolower(colnames(obj)), common)) > 0){
63+
warning("Ensure a value for 'freq' was supplied if your data was in frequency form.")
64+
}
65+
}
66+
return(tab)
67+
}

0 commit comments

Comments
 (0)