On 08/18/2010 07:51 PM, Chen,Shaofei wrote:
Consider an example:
There are 3 stores (1,2,3), and 5 customers. Each store must have at least
one customer. Thus a possible combination is 11213. On the other hand, a
combination 22333 is not what I want

I have considered your solution earlier, but in this case, first three
customers have to go to store 123 in sequence, and the randomness has been
reduced. You are right, the sampling procedure is not totally random, but I
want to be as random as possible.

So just use my previous solution and permute the result using sample.


Thanks!

-----Original Message-----
From: Erik Iverson [mailto:er...@ccbr.umn.edu]
Sent: Wednesday, August 18, 2010 7:11 PM
To: Chen,Shaofei
Cc: r-help@r-project.org
Subject: Re: [R] sample() question

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?


______________________________________________
R-help@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.

Reply via email to