Create the sample matrix from the population by using sample & replicate function

rsample

#1

I'm trying to create the matrix from the sample population that I created in my R.

I used the function "sample" and "replicate". Below is my code.

# Create the sample population
sample_popu <- c(8, 3, 1, 11, 4, 7)

# Create the sample value in the matrix form
matrix_sample_data <- matrix(replicate(30, (sample(sample_popu, 2, replace = FALSE))), nrow = 2, ncol = 30)

When I used the above code to generate 30 number of sample from my sample_popu, I'm seeing that a couple of sample values are replicated more than once.

I was wondering, is there any way that I can generate the sample matrix without replication? As far as I searched and my knowledge, I cannot use the replacement in the replicate function.

Thank you for your help in advance!


#2

If I'm understanding you properly, the problem is that there are repeated pairs of values in your matrix? E.g., [4, 8] appears twice, in columns 25 and 26.

There are only 30 possible permutations of pairs drawn from your 6 numbers, assuming that order matters (so [8, 4] is different from [4, 8]). I like using the arrangements package for combinatoric tasks (there are several other options: see this great review at Stack Overflow):

library(arrangements)

# Create the sample population
sample_popu <- c(8, 3, 1, 11, 4, 7)

# Find all the permutations of selecting 2 items
# from `sample_popu`. replace = FALSE by default
# (see ?arrangements::permutations)
permutations(sample_popu, k = 2, layout = "column")
#>      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13]
#> [1,]    8    8    8    8    8    3    3    3    3     3     1     1     1
#> [2,]    3    1   11    4    7    8    1   11    4     7     8     3    11
#>      [,14] [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23] [,24]
#> [1,]     1     1    11    11    11    11    11     4     4     4     4
#> [2,]     4     7     8     3     1     4     7     8     3     1    11
#>      [,25] [,26] [,27] [,28] [,29] [,30]
#> [1,]     4     7     7     7     7     7
#> [2,]     7     8     3     1    11     4

Created on 2018-09-24 by the reprex package (v0.2.1)

These are obviously not in a random order — arrangements::permutations() puts the results in lexicographic order by default. I don't know if that matters to you?


#3

Yes, you are right! @jcblum

Thank you for your help!