| crosstab {qacBase} | R Documentation |
Two-way frequency table
Description
This function creates a two way frequency table.
Usage
crosstab(
data,
rowvar,
colvar,
type = c("freq", "percent", "rowpercent", "colpercent"),
total = TRUE,
na.rm = TRUE,
digits = 2,
chisquare = FALSE,
plot = FALSE
)
Arguments
data |
data frame |
rowvar |
row factor (unquoted) |
colvar |
column factor (unquoted) |
type |
statistics to print. Options are |
total |
logical. if TRUE, includes total percents. |
na.rm |
logical. if TRUE, deletes cases with missing values. |
digits |
number of decimal digits to report for percents. |
chisquare |
logical. If |
plot |
logical. If |
Details
Given a data frame, a row factor, a column factor, and a type (frequencies, cell percents, row percents, or column percents) the function provides the requested cross-tabulation.
If na.rm = FALSE, a level labeled <NA> added. If
total = TRUE, a level labeled Total is added. If
chisquare = TRUE, a chi-square test of independence is
performed.
Value
If plot=TRUE, return a ggplot2 graph.
Otherwise the function return a list with 6 components:
table(table). Table of frequencies or percentstype(character). Type of table to printtotal(logical). IfTRUE, print row and or column totalsdigits(numeric). number of digits to printrowname(character). Row variable namecolname(character). Column variable namechisquare(character). Ifchisquare=TRUE, contains the results of the Chi-square test.NULLotherwise.
See Also
Examples
# print frequencies
crosstab(mtcars, cyl, gear)
# print cell percents
crosstab(cardata, vehicle_size, driven_wheels)
crosstab(cardata, vehicle_size, driven_wheels,
plot=TRUE)
crosstab(cardata, driven_wheels, vehicle_size,
type="colpercent", plot=TRUE, chisquare=TRUE)