The “table” Function in R
Package: Base R (no specific package required)
Purpose: Construct contingency tables of the counts at each combination of factor levels.
General Class: Descriptive Statistics
Required Argument(s):
...: One or more factors.
Notable Optional Arguments:
exclude: Levels to be excluded from the result. The default is NULL.
useNA: A character string specifying if and how to treat missing values. Options include “no”, “ifany”, “always”. Default is “ifany”.
dnn: A character vector specifying the names for the dimensions in the result. The default is NULL.
deparse.level: An integer specifying how the levels of factors in the result should be deparsed. The default is 1.
Example:
# Example data frame
data <- data.frame(
Gender = c("Male", "Female", "Male", "Female", "Male", "Female", "Male", "Female", "Male", "Female"),
Class = c("A", "B", "A", "B", "A", "B", "A", "B", "A", "B")
)
# Create a contingency table of counts
result <- table(data$Gender, data$Class)
print(result)In this example, the table function is used to create a contingency table of counts for the combination of the Gender and Class variables in the data frame. The result is then printed to the console.