Hello all, I have a question regarding sample() in R. For example, I have a set: set <- c(2,3,5) and I want to draw 5 samples from this set, so replacement is true: m <- sample(set, 5, replace=TRUE) However here comes a problem, for example, I will have (2,3,3,2,5), but I will also get (3,3,5,5,3) in some cases. This means element 2 has not been sampled in this case. The way I want to do is to random sample with replacement, but all elements have to be sampled. Any solutions? Thanks! Regards, Shaofei [[alternative HTML version deleted]]
On 08/18/2010 06:58 PM, Chen,Shaofei wrote:> Hello all, > > > > I have a question regarding sample() in R. For example, I have a set: > > set<- c(2,3,5) > > and I want to draw 5 samples from this set, so replacement is true: > > m<- sample(set, 5, replace=TRUE) > > However here comes a problem, for example, I will have (2,3,3,2,5), but I > will also get (3,3,5,5,3) in some cases. This means element 2 has not been > sampled in this case. > > The way I want to do is to random sample with replacement, but all elements > have to be sampled. > > Any solutions?Well, if all elements have to be sampled once, then those 3 elements will not be random, since they must be present. So you're really only sampling 2 elements from the set, so just do that. c(1:3, sample(set, 2, replace = TRUE) although I really wonder what you're trying to do here?
Just to clarify some of your language here before some others rip you apart, you mean to say you'd like to take a random sample of size 5 and not 5 random samples. Now, I believe you can control the probability with which each element of your original data set is sampled (using weights), but in order to obtain a random sample such that all values are included, then you must write the code and keep sampling until all elements appear, but your random sample size may be larger than size 5. On Aug 18, 2010, at 4:58 PM, Chen,Shaofei wrote: Hello all, I have a question regarding sample() in R. For example, I have a set: set <- c(2,3,5) and I want to draw 5 samples from this set, so replacement is true: m <- sample(set, 5, replace=TRUE) However here comes a problem, for example, I will have (2,3,3,2,5), but I will also get (3,3,5,5,3) in some cases. This means element 2 has not been sampled in this case. The way I want to do is to random sample with replacement, but all elements have to be sampled. Any solutions? Thanks! Regards, Shaofei [[alternative HTML version deleted]] ______________________________________________ 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.