[R] sampling without repetition

Thomas W Blackwell tblackw at umich.edu
Tue Nov 18 02:01:31 CET 2003


Rajarshi  -

Do you want three sets, three disjoint sets, or sets of
size three ?  It's not clear what you are attempting to do.

-  tom blackwell  -  u michigan medical school  -  ann arbor  -

On Mon, 17 Nov 2003, Rajarshi Guha wrote:

> Hi,
>   I'm trying to write a function that will divide a given range of
> numbers into 3 sets using sample(), without repetition. Currently I'm
> trying this approach:
>
> r <- 1:10
> s1 <- sample(r,size=3)
>
> Next, I want to remove the selected elements from r and sample() from
> the remainder.
>
> r <- r[ -(r=s1) ]
> s2 <- sample(r,size=3)
>
> When I go to remove the elements contained in s2 from r I get an error:
>
> r <- r[ -(r=s2) ]
> Error: subscript out of bounds
>
> I'm not sure why this is happening. I tried replacing the '=' with '=='
> but I get another error
>
> Warning message:
> longer object length
>         is not a multiple of shorter object length in: r == s1
>
> Essentially what I need is to get the indices into r of the elements of
> s1 & s2. I have looked at which but I cant seem to work out how I can
> get the indices into r of all the elements of, say, s1.
>
> Does anybody have any suggestions? (Of course if there is a more elegant
> way of doing this whole thing I would appreciate any pointers)
>
> Thanks,
>
> -------------------------------------------------------------------
> Rajarshi Guha <rxg218 at psu.edu> <http://jijo.cjb.net>
> GPG Fingerprint: 0CCA 8EE2 2EEB 25E2 AB04 06F7 1BB9 E634 9B87 56EE
> -------------------------------------------------------------------




More information about the R-help mailing list