library(vcdExtra)
data("HairEyeColor")
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)
Run the code above in your browser using DataLab