I'd like to sample a vector x of length 7 with replacement and sample that vector 10 separate times. I've tried the something like the following but can't get the resulting 7x10 output I'm looking for. This produces a 1x7 vector but I can't figure out to get the other 9 vectors
x <- runif(7, 0, 1)
for(i in 1:10){
samp <- sample(x, size = length(x), replace = T)
}
Sampling with replacement simply means that each number is “replaced” after it is selected, so that the same number can show up more than once. This is what we want here, since what you roll on one die shouldn't affect what you roll on any of the others.
To do this, use the set. seed() function. Using set. seed() will force R to produce consistent random samples at any time on any computer.
When you sample replace = False, first element/number picked for sampling will not kept back in entire population to be picked again in same sample.
permn() method in R generates all permutations of the elements of x. If x is a positive integer, returns all permutations of the elements of seq(x).
This is a very convenient way to do this:
replicate(10,sample(x,length(x),replace = TRUE))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With