[R] Sampling according to type

2014-03-05 Thread Thomas
I have a matrix where each entry represents a data subject's type, 1 or 0: n - 10 ntype - rbinom(n, 1, 0.5) and I'd like to sample say 3 subjects from ntype where those subjects who are Type 1 are selected with probability say 0.75, and Type 0 with (1-0.75). (So the sample would produce a

Re: [R] Sampling according to type

2014-03-05 Thread Suzen, Mehmet
If I understood correctly, you need weighted sampling. Try 'prob' argument from 'sample'. For your example: n - 10 ntype - rbinom(n, 1, 0.5) myProbs - rep(1/10, 10) # equally likely myProbs[ which(ntype == 0)] - 0.75/7 # Divide so the sum will be 1.0 myProbs[ which(ntype == 1)] - 0.25/3

Re: [R] Sampling according to type

2014-03-05 Thread Suzen, Mehmet
myProbs[ which(ntype == 0)] - 0.75/7 # Divide so the sum will be 1.0 myProbs[ which(ntype == 1)] - 0.25/3 Here of course you need to divide by number of 0s and 1s, 7 and 3 were was just an example. __ R-help@r-project.org mailing list