Aleksey Naumov
2000-Nov-17 01:01 UTC
[R] Randomly selecting n (unique) elements out of vector of length m...
Dear R users, I wonder if there is a simple way to select a random and non-overlapping subset of a vector? Say, I need to randomly select 10 out of 100 numbers in a vector V. By "non-overlapping" I mean that a number in any position in V (V[1]...V[100]) should be selected at most once (i.e. selection without recycling). I could simplu use a uniform distribution to generate selection indices:> floor( runif(10, min=1, max=101) )[1] 17 34 16 91 43 2 22 75 59 38 but there is no guarantee that elements will be unique, so I'll have to check and add some if needed (and probably do it a few times)... Is there a simper way? Thanks, Aleksey -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Ross Ihaka
2000-Nov-17 01:51 UTC
[R] Randomly selecting n (unique) elements out of vector of length m...
On Thu, Nov 16, 2000 at 08:01:23PM -0500, Aleksey Naumov wrote:> Dear R users, > > I wonder if there is a simple way to select a random and non-overlapping > subset of a vector? > Say, I need to randomly select 10 out of 100 numbers in a vector V. By > "non-overlapping" I > mean that a number in any position in V (V[1]...V[100]) should be > selected at most once > (i.e. selection without recycling). > > I could simplu use a uniform distribution to generate selection indices: > > > floor( runif(10, min=1, max=101) ) > [1] 17 34 16 91 43 2 22 75 59 38 > > but there is no guarantee that elements will be unique, so I'll have to > check and add some if > needed (and probably do it a few times)... > > Is there a simper way?> sample(1:101, 10, replace=FALSE)[1] 77 56 84 6 2 50 63 99 35 11 Ross -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Aleksey Naumov
2000-Nov-17 03:39 UTC
[R] Randomly selecting n (unique) elements out of vector of length m...
Thank you all very much! The answer was simple, but it just wasn't clear where to look for it (I checked in "Keywords by Topic", "An Introduction to R", and "Reference Manual") :-) Aleksey ------- Try the function sample(). In your case... subset <- sample(V,size=10,replace=FALSE) Cheers, Pierre Aleksey Naumov wrote:> Dear R users, > > I wonder if there is a simple way to select a random and non-overlapping > subset of a vector? > Say, I need to randomly select 10 out of 100 numbers in a vector V. By > "non-overlapping" I > mean that a number in any position in V (V[1]...V[100]) should be > selected at most once > (i.e. selection without recycling). > > I could simplu use a uniform distribution to generate selection indices: > > > floor( runif(10, min=1, max=101) ) > [1] 17 34 16 91 43 2 22 75 59 38 > > but there is no guarantee that elements will be unique, so I'll have to > check and add some if > needed (and probably do it a few times)... > > Is there a simper way? > > Thanks, > Aleksey > > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- > r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html > Send "info", "help", or "[un]subscribe" > (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._