Re: [R] Help with prob in sample()

2014-03-07 Thread Greg Snow
Oops, my answer was for when replace=TRUE, when replace=FALSE it uses a different method, but that method is described on the help page for sample. Essentially it chooses the first number, then removes that value from x and prob, then chooses the next (rescaling prob again), etc. On Fri, Mar 7, 2

Re: [R] Help with prob in sample()

2014-03-07 Thread Greg Snow
Essentially what the sample function is doing (though it does it in a much more efficient way I expect) is the equivalent of this code: i <- c(1:10) myProbs <- c(0.1, 0.1, 0.1, 0.1, 0.1, 0.9, 0.9, 0.9, 0.9, 0.9) myProbs <- myProbs/sum(myProbs) cp <- c(0,cumsum(myProbs)) i[findInterval( runif(5),

[R] Help with prob in sample()

2014-03-07 Thread Thomas
I'm trying to figure out exactly what the prob parameter in the sample function does. With the following code, does sample look randomly for the first possible sample--let's say it choses the second element--and then assess whether it can be chosen according to it's probability which is 0