Creating Two-Way Tables ManuallyΒΆ

Here is an example showing how to do this. Once you have the matrix, you can use it to do things like a chi-squared test for two-way tables. This example uses a sample of regular and peanut M&Ms.

In [1]:
colors=c("Brown","Yellow","Red","Orange","Blue","Green")
A=matrix(c(5,6,7,8,9,10,1,2,3,4,5,6),nrow=2,byrow='True')
colnames(A)=colors
rownames(A)=c("Regular","Peanut")
A
Out[1]:
BrownYellowRedOrangeBlueGreen
Regular 5 6 7 8 910
Peanut123456

Right now, the table is actually an R matrix. To change the class to the table class in R, use the command as.table().

In [3]:
as.table(A)
Out[3]:
        Brown Yellow Red Orange Blue Green
Regular     5      6   7      8    9    10
Peanut      1      2   3      4    5     6