Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I split a vector into two columns to create ordered pairs for random assignment

I am trying to generate random pairs from 34 subjects for an experiment. Subjects will be assigned ID #'s 1-34. To generate the random ordered numbers (1-34) I used the following code:

  ### Getting a vector of random ordered numbers 1-34###
  pairs<-sample(1:34,34,replace=F)
  pairs
  [1] 16 22  8 13  4 25 18 12 17  5  6 31 29 27 30 23  2 14  9 24 34 21 11  
    3  1 28 33 20 32 26 19 10 15  7

What I would like to do is to take this random ordering of numbers and split every other element of the vector into a column so that I get the following ordered pairs:

   partner1     partner2
    16           22
     8           13
     .            .
     .            .
    15            7

Thoughts or ideas on how to go from the vector to the ordered pairs? Any help or insight would be much appreciated.

-Thomas

like image 919
Thomas Avatar asked Jul 09 '10 11:07

Thomas


1 Answers

That could be as easy as

 newpairs <- matrix(pairs, ncol=2, byrow=TRUE)

and each row then gives you the pair.

Edit Correct to use matrix()

like image 122
Dirk Eddelbuettel Avatar answered Oct 05 '22 19:10

Dirk Eddelbuettel