[R] symmetric (& square) contingency table from dataset of unordered pairs

Daniel Weitzenfeld dweitzenfeld at gmail.com
Fri Mar 25 05:21:09 CET 2011


Hi Everybody,
I have a data set in which each observation has a pair of students,
with each kid id'd by a 4 digit number:

> head(PAIRS)
  student1 student2
2     2213     2200
4     2198     2195
5     2199     2191
6     2229     2221
7     2247     2249
8     2250     2263

There is no significance to student1 vs. student2:  they are just a
pair, and the "variable" names could be flipped without loss of
meaning.

I want a symmetric, square contingency table with entry(i,j) = number
of times students i and j are paired together.
BUT because some students appear only in student1 and others only in
student2, table() produces rectangular, asymmetric tables; row.names
!= col.names.  I can't figure out how to get R to ignore treat the
observations as unordered pairs.

Currently the student ids are numeric; is the solution to treat them
as factors, and ensure that the set of levels for each factor is
identical?

I have a kludgey hack - stack the PAIRS dataset on top of a reversed
(student2=student1 and vice versa) version of itself, then use
table().  But I'm wondering if there's a more elegant way.

Thanks,
Dan



More information about the R-help mailing list