[R] sample() question
Chen,Shaofei
sxbxchen at hotmail.com
Thu Aug 19 03:19:25 CEST 2010
Yes it is! I just came up with a method, which needs 5 lines.
Thank you very much!
-----Original Message-----
From: jim holtman [mailto:jholtman at gmail.com]
Sent: Wednesday, August 18, 2010 8:10 PM
To: Chen,Shaofei
Cc: R Newbie; r-help at r-project.org
Subject: Re: [R] sample() question
I think this is what was previously proposed on the list; does this do
it for you:
> sample(c(1:3, sample(1:3,2,TRUE)), 5)
[1] 1 2 3 1 1
> sample(c(1:3, sample(1:3,2,TRUE)), 5)
[1] 3 2 1 2 3
> sample(c(1:3, sample(1:3,2,TRUE)), 5)
[1] 1 2 3 2 1
> sample(c(1:3, sample(1:3,2,TRUE)), 5)
[1] 1 2 3 2 2
> sample(c(1:3, sample(1:3,2,TRUE)), 5)
[1] 2 3 3 2 1
> sample(c(1:3, sample(1:3,2,TRUE)), 5)
[1] 3 1 3 1 2
On Wed, Aug 18, 2010 at 8:56 PM, Chen,Shaofei <sxbxchen at hotmail.com> wrote:
> 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.
>
> ______________________________________________
> 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.
>
--
Jim Holtman
Cincinnati, OH
+1 513 646 9390
What is the problem that you are trying to solve?
More information about the R-help
mailing list