R - how to sample pairs from a vector without repeating values?

3

I'm trying to create 100 pairs from a vector with 200 values. I have constructed a vector following a normal distribution as follows:

vetor=rnorm(200,mean=30,sd=6)

And now I want to extract 100 pairs of these values, but without repeating any of them. I tried to do it as follows:

pares=replicate(100,sample(vetor,2,replace=FALSE))

In this case, replace=FALSE prevented equal pairs from being sampled, but some vector values entered more than one pair, and other values did not match at all.

What I want is for all values to be part of some pair and for all pairs to be different.

Could you please help me?

    
asked by anonymous 10.02.2018 / 19:26

1 answer

5

It's easier to do this than you think. More precisely, you do not have to use replicate . Just see that sample(vetor) produces a permutation of its argument. (No repetitions.)

set.seed(3604)    # para que os resultados sejam reprodutíveis 
vetor <- rnorm(200, mean = 30, sd = 6)

pares <- matrix(sample(vetor), nrow = 2)
    
10.02.2018 / 19:38