Daniel Weitzenfeld
2011-Mar-25 04:21 UTC
[R] symmetric (& square) contingency table from dataset of unordered pairs
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
Apparently Analagous Threads
- "disappeared" user
- ActiveRecords: save & relationships
- Is there a function to interdigitate two columns?
- different logon path for different users - local profiles for a few users only - how?
- Sharing of Directories & assigning group permissions to directories