[R] Sampling according to type

Suzen, Mehmet msuzen at gmail.com
Wed Mar 5 16:46:30 CET 2014


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
sample(ntype,3, prob=myProbs)




On 5 March 2014 15:20, Thomas <Thomas.Chesney at nottingham.ac.uk> wrote:
> 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 list with three indices each referring to a
> position within ntype.)
>
> Can anyone suggest a way to do this please?
>
> Thank you,
>
> Thomas Chesney
> This message and any attachment are intended solely for the addressee and
> may contain confidential information. If you have received this message in
> error, please send it back to me, and immediately delete it.   Please do not
> use, copy or disclose the information contained in this message or in any
> attachment.  Any views or opinions expressed by the author of this email do
> not necessarily reflect the views of the University of Nottingham.
>
> This message has been checked for viruses but the contents of an attachment
> may still contain software viruses which could damage your computer system,
> you are advised to perform your own checks. Email communications with the
> University of Nottingham may be monitored as permitted by UK legislation.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list