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

\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 matrix 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 matrix form.
}
\description{
Converts object (\code{obj}) in frequency, case or table form into a matrix of
specified dimensions (\code{dims}). The column containing the frequencies (\code{freq})
must be supplied if \code{obj} is in frequency form.
}
\details{
First converts \code{obj} into an array using \code{\link{as_array}}. Then a
check is made to ensure the user inputted a 2D \code{obj}. If \code{obj} is not 2D, an
error is returned. If \code{obj} is 2D, \code{\link[base]{as.matrix}} is applied.
}
\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
arrayDat <- as_array(HairEyeColor) # Generate an array

# Table form -> matrix
as_matrix(HairEyeColor, dims = c("Hair", "Sex")) |> str()

# Frequency form -> matrix
as_matrix(freqForm, freq = "Freq", dims = c("Hair", "Sex")) |> str()

# Case form -> matrix form
as_matrix(caseForm, dims = c("Hair", "Sex")) |> str()

# Frequency (tibble) form -> matrix form
as_matrix(tidy_freqForm, freq = "n", dims = c("Hair", "Sex")) |> str()

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

# Proportions relative to grand total
as_matrix(HairEyeColor, dims = c("Hair", "Sex"), prop = TRUE)

# Marginalize proportions along "Sex" (i.e., male proportions sum to 1, 
# female proportions sum to 1)
as_matrix(HairEyeColor, dims = c("Hair", "Sex"), prop = "Sex")

as_matrix(HairEyeColor, dims = c("Hair", "Sex"), prop = 2) # Same as above


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