## Creating a two-way table by listing cells by rows.
t1 = matrix(c(37, 91,22, 15, 46, 25), ncol = 3, byrow = TRUE)
colnames(t1) = c("Front", "Middle", "Back")
rownames(t1) = c("Female", "Male")
t1 = as.table(t1)
t1
## Front Middle Back
## Female 37 91 22
## Male 15 46 25
summary(t1)
## Number of cases in table: 236
## Number of factors: 2
## Test for independence of all factors:
## Chisq = 7.474, df = 2, p-value = 0.02383
A factor is a categorical variable.
totPercents(t1,1)
## Front Middle Back Total
## Female 15.7 38.6 9.3 63.6
## Male 6.4 19.5 10.6 36.4
## Total 22.0 58.1 19.9 100.0
rowPercents(t1,1)
## Front Middle Back Total Count
## Female 24.7 60.7 14.7 100.1 150
## Male 17.4 53.5 29.1 100.0 86
mosaicplot(t1, main = "Conditional distribution of column variable for row levels",
xlab = "Seating", ylab = "Gender", dir = c("h","v"))
colPercents(t1,1)
## Front Middle Back
## Female 71.2 66.4 46.8
## Male 28.8 33.6 53.2
## Total 100.0 100.0 100.0
## Count 52.0 137.0 47.0
mosaicplot(t1, main = "Conditional distribution of row variable for column levels",
xlab = "Seating", ylab = "Gender", dir = c("h","v"), sort = c(2,1))
The above was modeled from examples at https://www.cyclismo.org/tutorial/R/tables.html#creating-a-table-directly. https://rdrr.io/cran/RcmdrMisc/man/colPercents.html