[R] How Can I Concatenate Every Row in a Data Frame with Every Other Row?

Donald Macnaughton donmac at matstat.com
Sat Mar 21 20:46:39 CET 2009

On Sat, Mar 21, 2009 at 12:01 PM, I wrote:
> I have a data frame with roughly 500 rows and 120 variables.  
> I would like to generate a new data frame that will include 
> one row for each PAIR of rows in the original data frame and 
> will include all 120 + 120 = 240 variables from the two rows.  
> I need only one row for each pair, not two rows.  Thus the 
> new data frame will contain 500 x 499 / 2 = 124,750 rows.
> Is there an easy way to do this with R?
> Thanks in advance,
> Don Macnaughton

I thank David Wisemius, Duncan Murdoch, and Jim Holtman for their helpful
replies.  Jim wrote

> What is the problem that you are trying to solve?

This work is for a client whose son was accused of cheating on a multiple
choice exam.  One can investigate this matter statistically by computing
the number of matching answers to questions on the exam between all pairs
of students.  Of course under the null hypothesis of no cheating the number
of matching answers has a certain distribution, which allows one to reject
the null hypothesis if the number of matching answers is unduly large for a
particular pair.  (The distribution is generally taken with respect to the
average number of correct answers in a given pair because the more correct
answers, the more matches can be expected under the null hypothesis.)

Wesolowsky (2000) discusses some of the statistical and ethical aspects of
this exercise.

Don Macnaughton


Wesolowsky, G. O. 2000. Detecting excessive similarity in answers on
multiple choice exams.  _Journal of Applied Statistics,_ 27, 909-921.

More information about the R-help mailing list