How to split a vector into two columns to create randomly assigned ordered pairs

I tried to generate random pairs from 34 subjects Subjects will be assigned ID #1-34 To generate 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 want to do is to use random sorting of numbers and divide each other element of the vector into a column to get the following ordered pairs:

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

Thoughts or ideas on how to go from vector to ordered pair? Any help or advice will be appreciated

– Thomas

Solution

This may be simple

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

Then each line will give you a pair

Edit correct use matrix ()

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>