R/table.multi.R
cross.multi.table.Rd
This function allows to generate a two-way frequency table from a multiple choices question and a factor. The question's answers must be stored in a series of binary variables.
cross.multi.table(
df,
crossvar,
weights = NULL,
digits = 1,
freq = FALSE,
tfreq = "col",
n = FALSE,
na.rm = TRUE,
...
)
data frame with the binary variables
factor to cross the multiple choices question with
optional weighting vector
number of digits to keep in the output
display percentages
type of percentages to compute ("row" or "col")
if TRUE
, and freq is TRUE
, display number of observations per row or column
Remove any NA values in crossvar
arguments passed to multi.table
Object of class table.
See the multi.table
help page for details on handling of the multiple
choices question and corresponding binary variables.
If freq
is set to TRUE, the resulting table gives the columns percentages
based on the contingency table of crossvar in the respondants population.
## Sample data frame
set.seed(1337)
sex <- sample(c("Man","Woman"),100,replace=TRUE)
jazz <- sample(c(0,1),100,replace=TRUE)
rock <- sample(c(TRUE, FALSE),100,replace=TRUE)
electronic <- sample(c("Y","N"),100,replace=TRUE)
weights <- runif(100)*2
df <- data.frame(sex,jazz,rock,electronic,weights)
## Two-way frequency table on 'music' variables by sex
cross.multi.table(df[,c("jazz", "rock","electronic")], df$sex, true.codes=list("Y"))
#> Man Woman
#> jazz 19 28
#> rock 23 26
#> electronic 25 26
## Column percentages based on respondants
cross.multi.table(df[,c("jazz", "rock","electronic")], df$sex, true.codes=list("Y"), freq=TRUE)
#> Man Woman
#> jazz 43.2 50.0
#> rock 52.3 46.4
#> electronic 56.8 46.4
## Row percentages based on respondants
cross.multi.table(df[,c("jazz", "rock","electronic")],
df$sex, true.codes=list("Y"), freq=TRUE, tfreq="row", n=TRUE)
#> Man Woman n
#> jazz 40.4 59.6 47
#> rock 46.9 53.1 49
#> electronic 49.0 51.0 51