I’m trying to sample from a multinomial in order to pull out elements of a vector by their weight.
For example, given the vector v <- c(10, 30, 60) I would like something like:
[ 3, 3, 2, 3, 1, 2, 3, 2, 3, 3 ]
Here’s the best I can come up with so far:
v <- c(10, 30, 60)
apply(rmultinom(10,1,v),2,function(x) which(x==1))
This works fine for small N and K, but I actually need to generate 100k samples from an index set from 1 to 100k, since this method requires a NxK temporary matrix it is obviously not going to work. I could just use a for loop as well, but I was hoping there is existing methods to do this.
You can just use
samplewith the prob parameterSome sample output: