Learn R Programming

vcdExtra (version 0.9.6)

as_freqform: Convert any form (case or table form) into frequency form.

Description

A wrapper for 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.

Usage

as_freqform(obj, freq = NULL, dims = NULL, prop = NULL, tidy = TRUE)

Value

Object in frequency form.

Arguments

obj

Object to be converted to frequency form.

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").

dims

A character vector of dimensions. If not specified, all variables apart from freq will be used as dimensions.

prop

If set to TRUE, the resulting "frequency" column will contain proportions (that sum to 1). May also be set to a character or numeric vector of dimensions to be used as margins from which proportions will be computed. The resulting "frequency" column is renamed to "Prop."

tidy

Returns a tibble if set to TRUE.

Author

Gavin M. Klorfine

Details

Converts obj to a table using as_table before converting to frequency form

See Also

as_table, as_caseform, as_array, as_matrix

Examples

Run this code
library(vcdExtra)

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

# 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()

#-----For proportions-----#

as_freqform(tableForm, prop = TRUE) |> head() # print only Sex == Male rows

# Marginalize proportions along "Sex" (i.e., male proportions sum to 1, female proportions sum to 1)
as_freqform(tableForm, prop = "Sex") |> head()

as_freqform(tableForm, prop = 3) |> head() # Same as above

# Marginalize proportions along multiple variables
as_freqform(tableForm, prop = c("Hair", "Sex")) |> head()

as_freqform(tableForm, prop = c(1, 3)) |> head() # Same as above

# Using dims and prop arguments in tandem
as_freqform(tableForm, dims = c("Hair", "Eye"), prop = TRUE)

Run the code above in your browser using DataLab