% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/as_table.R
\name{as_table}
\alias{as_table}
\title{Convert frequency or case form data into table form}
\usage{
as_table(obj, freq = NULL, dims = NULL, prop = NULL)
}
\arguments{
\item{obj}{Object to be converted to table form.}

\item{freq}{If \code{obj} is in frequency form, this is the name of the frequency column.
Leave as \code{NULL} if \code{obj} is in any other form.}

\item{dims}{A character vector of dimensions. If not specified, all variables apart from
\code{freq} will be used as dimensions.}

\item{prop}{If set to \code{TRUE}, returns a table of 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.}
}
\value{
Object in table form.
}
\description{
Converts object (\code{obj}) in frequency or case form into table form. The
column containing the frequencies (\code{freq}) must be supplied if \code{obj} is in
frequency form. Optionally returns a table of proportions with (optionally) specified margins.
}
\details{
If \code{obj} was in table form to begin with, it is returned to the user as-is
unless dimensions were specified (in which case it returns a table with
entries summed over excluded dimensions). When \code{prop} is set to \code{TRUE}, the
returned table will have proportions that sum to one, whereas if a character
or numerical vector of table dimensions is supplied to \code{prop}, proportions
will be marginalized across the specified dimensions.
}
\examples{
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)


}
\seealso{
\code{\link{as_freqform}}, \code{\link{as_caseform}}, \code{\link{as_array}},
\code{\link{as_matrix}}
}
\author{
Gavin M. Klorfine
}
