Section 9 Table

9.1 Function

Example vector: x <- sample(1:100, size=30); y <- rep(c('R1','R2','R3'), each=4); z <- rep(c(T,F), length=4)

Function Explanation
table Cross-tabulation and contingency table
ftable Create flat table
xtable Create cross table - data.frame to array
as.data.frame.table Create array to data.frame
prop.table Get table cells as proportions
margin.table Get marginal sums

9.2 Example

?table
?ftable
?xtable
?prop.table

set.seed(12345)
x <- sample(1:100, size=30)
y <- array(data=x, dim=c(5,3,2))
dimnames(y) <- list(Region=LETTERS[1:5], 
                    AgeGr=c('Baby','Child','Young'), 
                    Status=c('Healthy','Infected'))
y

dimnames(y)
attributes(y)

table(y)
ftable(y)

z <- as.data.frame.table(y)
names(z) <- c('Location','Group','Status','Freq')

xtabs(Freq ~ ., z)

a <- y[1:5,1:3,2]
a <- as.table(a)
a

prop.table(a)
addmargins(a)
prop.table(a, margin=1)
round(prop.table(a, margin=1)*100,2)