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

David Winsemius dwinsemius at comcast.net
Sat Mar 21 17:41:54 CET 2009


I hacked at a bit differently than Duncan. See if these help pages and  
this example point another way:

?combn
?"["


 > df <- data.frame(a = 1:4, b=LETTERS[1:4])
 > n <- nrow(df)
 > cbind(df[combn(1:n,2)[1,],], df[combn(1:n,2)[2,],] )
     a b a b
1   1 A 2 B
1.1 1 A 3 C
1.2 1 A 4 D
2   2 B 3 C
2.1 2 B 4 D
3   3 C 4 D


-- 
David Winsemius

On Mar 21, 2009, at 12:01 PM, Donald Macnaughton 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?
>


David Winsemius, MD
Heritage Laboratories
West Hartford, CT




More information about the R-help mailing list