[R] Constructing a contingency table

Andrew Perrin aperrin at socrates.berkeley.edu
Mon Apr 2 23:12:05 CEST 2001

Greetings. I'm having some trouble constructing a contingency table from
raw data (actually read in via RPgSQL).  Here's the deal:

- What I've got: a data frame in the form:

Groupcode  code1  code2  code3  code4  code5 .. coden

where groupcode is one of {P,C,B,S,U,X} and code{1..n} is TRUE or
FALSE. code{1..n} are NOT mutually exclusive, so between 0 and n of them
can be TRUE.

- What I need: a contingency table in the form:

Groupcode  code1  code2 .. coden

with each cell in the table being a count of TRUE values where groupcode
is the corresponding groupcode.

The table will eventually be analyzed using chisq.test.

I've tried using table() but It's not immediately clear how to include
values from two or more variables in a data frame.

Many thanks.

Andrew J Perrin - Ph.D. Candidate, UC Berkeley, Dept. of Sociology  
(Soon: Asst Professor of Sociology, U of North Carolina, Chapel Hill)
andrew_perrin at unc.edu - http://www.unc.edu/~aperrin

r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch

More information about the R-help mailing list