Generate multiple samples from the same vector

Asked

Viewed 70 times

4

I’m a beginner in R. I’m trying to do a function to generate as many samples as possible without repeating size 4 taken from the same vector. I tried to loop the for, but I can’t do the whole thing. The vector I want to take the samples from follows below and the function I did too:

y<-c(1,2,4,4,7,7,7,8)
espaco_amostral<-function(x,n){
set.seed(360)
   for(i in x){ 
     amostra<-sample(x,n,replace=F)
   }
 matrix(amostra,,n)
}
espaco_amostral(y,4)

The idea is that the various samples stay in the matrix lines.

  • What does it mean to "generate as many size 4 samples taken from the same vector as possible"? This number is infinite because given the vector c(1,2,4,4,7,7,7,8) from your example, I can generate the samples 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1... and so on, no limit. They all have size 4. Do the samples have to be unique? Can they have repeated values? You need to define your problem a little better.

  • 1

    I forgot to say it would be repeating samples. In case it would be all size four samples that can be taken from the vector without repeating. For example, using another vector x<-(1,2,3,4), the two possible size samples without replacement are: (1,2), (1,3), (1,4), (2,3), (2,4), (3,4). I was able to clarify?

  • Yes, now it’s clear. I gave a possible solution to the problem just below.

  • 1

    Thanks, helped!

  • It’s great to know that my response has helped you in some way. So consider vote and accept the answer, so that in the future other people who experience the same problem have a reference to solve it.

1 answer

4

This problem can be solved with the function combn. Just inform the vector that owns your population (x) and the sample size to be generated (2):

x <- c(1,2,3,4)
t(combn(x, m=2))
     [,1] [,2]
[1,]    1    2
[2,]    1    3
[3,]    1    4
[4,]    2    3
[5,]    2    4
[6,]    3    4

As the function combn informs the combinations per column, I needed to transpose the resulting data frame using the function t, so that the presentation is per line.

To solve this problem with your original data, just run the code below:

y <- c(1,2,4,4,7,7,7,8)
t(combn(y, m=4))

Browser other questions tagged

You are not signed in. Login or sign up in order to post.