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 -> 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()
#-----For proportions-----#
as_array(freqForm, freq = "Freq", prop = TRUE) |> # proportions relative to grand total
head(c(4,4,1))
# Marginalize proportions along "Sex" (i.e., male proportions sum to 1, female proportions sum to 1)
as_array(freqForm, freq = "Freq", prop = "Sex") |> head(c(4,4,1))
as_array(freqForm, freq = "Freq", prop = 3) |> head(c(4,4,1)) # Same as above
# Marginalize proportions along multiple variables
as_array(freqForm, freq = "Freq", prop = c("Hair", "Sex")) |> head(c(4,4,1))
as_array(freqForm, freq = "Freq", prop = c(1, 3)) |> head(c(4,4,1)) # Same as above
# Using dims and prop arguments in tandem
as_array(freqForm, freq = "Freq", dims = c("Hair", "Eye"), prop = TRUE)
Run the code above in your browser using DataLab