Problem
We want to tranform a contingency table into a frequency table in R.
# Contingency table
tbl <- table (mtcars[, c("am", "gear")])
gear
am 3 4 5
0 15 4 0
1 0 8 5
Frequency tables
We transform the contingency table into a data frame.
df <- as.data.frame(tbl)
df
am gear Freq
1 0 3 15
2 1 3 0
3 0 4 4
4 1 4 8
5 0 5 0
6 1 5 5
Transforming frequency tables in contingency tables
To transform a frequency table back to a contingency table.
ftable(xtabs(Freq ~ am + gear, data = df))
gear 3 4 5
am
0 15 4 0
1 0 8 5
It is the equivalent of:
ftable(mtcars[, c("am", "gear")])
References
No hay comentarios:
Publicar un comentario