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

From: Daniel Weitzenfeld <dweitzenfeld_at_gmail.com>
Date: Fri, 25 Mar 2011 15:21:09 +1100

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.


R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Fri 25 Mar 2011 - 04:29:19 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 25 Mar 2011 - 04:30:24 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive