[R] sample() question

Chen,Shaofei sxbxchen at hotmail.com
Thu Aug 19 02:56:57 CEST 2010


Yes, sample size is 5, but the size of set is 3.
There is no probability to control, or we can say all elements in the set
have equal probability to be picked, but I want everyone has to be sampled
at least once.
Thanks!

-----Original Message-----
From: R Newbie [mailto:help0938450 at gmail.com] 
Sent: Wednesday, August 18, 2010 7:12 PM
To: Chen,Shaofei
Cc: r-help at r-project.org
Subject: Re: [R] sample() question

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

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