The Art of R Programming

(WallPaper) #1

Vote.for.X Voted.for.X.Last.Time
1 Yes Yes
2 Yes No
3No No
4 Not Sure Yes
5No No


We can use thetable()function to compute the contingency table for
this data:



cttab <- table(ct)
cttab
Voted.for.X.Last.Time
Vote.for.X No Yes
No 2 0
Not Sure 0 1
Yes 1 1



The 2 in the upper-left corner of the table shows that we had, for exam-
ple, two people who said “no” to the first and second questions. The 1 in the
middle-right indicates that one person answered “not sure” to the first ques-
tion and “yes” to the second question.
We can also get one-dimensional counts, which are counts on a single
factor, as follows:



table(c(5,12,13,12,8,5))



5 8 12 13
2121

Here’s an example of a three-dimensional table, involving voters’ gen-
ders, race (white, black, Asian, and other), and political views (liberal or
conservative):



v # the data frame
gender race pol
1MWL
2MWL
3FAC
4MOL
5FBL
6FBC
vt <- table(v)
vt
,,pol=C



Factors and Tables 129
Free download pdf