[R] sample() question

Erik Iverson eriki at ccbr.umn.edu
Thu Aug 19 02:11:29 CEST 2010


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?



More information about the R-help mailing list