expand_freq_table | R Documentation |
expand_freq_table
turns a contingency table
(given as an array/table or as a data frame with a frequency count variable
freq_var
that denotes the number of cases for each factor combination)
into a data frame of raw cases.
expand_freq_table(x, freq_var = "Freq", row_name_repair = TRUE)
x |
An contingency table (as |
freq_var |
The name of the frequency count variable in
a data frame |
row_name_repair |
Boolean: Should rows be repaired (i.e., enumerated)?
Default: |
expand_freq_table
assumes that x
is a table
or a data.frame
with a frequency count variable freq_var
.
If x
is a table
, expand_freq_table
first uses
as.data.frame
(with responseName = freq_var
)
to turn x
into a contingency table (as a data frame with
a frequency variable, named "Freq"
by default).
The function allows turning a contingency table
(i.e., a table that cross-classifies frequency counts) —
in the form of a table
or a data.frame
with a frequency count variable freq_var
—
into a corresponding data.frame
of raw cases.
The number of cases or observations (rows) in the resulting data frame
corresponds to sum(x)
(for tables) or
sum(x$freq_var)
(for data frames).
A data frame (of raw cases).
table
and xtabs
for turning data frames into contingency tables;
as.data.frame
for turning an array/table into a contingency table (as df).
Other array functions:
add_dimnames()
,
ctable()
,
flatten_array()
,
subtable()
# (a) from raw data (vectors):
ans <- sample(c("yes", "no", "maybe"), 100, replace = TRUE)
eat <- sample(c("fish", "meat", "veggie"), 100, replace = TRUE)
df_1 <- data.frame(ans, eat) # data frame from vectors
df_2 <- expand_freq_table(data.frame(table(ans, eat))) # table > contingency table > df
all.equal(table(df_1), table(df_2))
# (b) from a table (3D table > contingency table > data frame > 3D array):
df <- expand_freq_table(UCBAdmissions) # array/table > contingency table > df
tb <- table(df) # df > array/table
all.equal(UCBAdmissions, tb)
# Trivial case:
expand_freq_table(data.frame(x = "a", Freq = 2))
# Full circle (4D array > contingency table > data frame > 4D array):
df <- expand_freq_table(Titanic)
tb <- table(df)
all.equal(Titanic, tb)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.