Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: Sample a vector with replacement multiple times

Tags:

r

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)
}
like image 733
srmulcahy Avatar asked Apr 21 '12 23:04

srmulcahy


People also ask

What is sampling with replacement in R?

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.

How do you generate a random sample in R?

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.

What does replace false do in R?

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.

Which command generate random permutations R?

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).


1 Answers

This is a very convenient way to do this:

replicate(10,sample(x,length(x),replace = TRUE))
like image 167
joran Avatar answered Oct 26 '22 13:10

joran