[R] all possible subsets of r out of n

Warnes, Gregory R gregory_r_warnes at groton.pfizer.com
Wed Aug 28 19:50:07 CEST 2002


The gregmisc package contains the function 'combinations'.

One you install gregmisc, the code

	library(gregmisc)
	combinations(10, 3, letters[1:10])

should give you what you want.

-Greg 

> -----Original Message-----
> From: Yuelin Li [mailto:yuelin at mail.med.upenn.edu]
> Sent: Wednesday, August 28, 2002 12:28 PM
> To: r-help at stat.math.ethz.ch
> Subject: [R] all possible subsets of r out of n
> 
> 
> Has anyone written a function that returns all possible
> subsets of r elements out of a vector that contains n
> elements?  I have not been able to find an answer in the FAQ
> or the archives.
> 
> Say I have a vector: letters[1:10].  I want to get a matrix
> of 120 rows that contains all possible subsets of 3 letters
> out of the 10.  And I would like to be able to generalize to
> any r out of n, like function(x=letters[1:10], r=3) that
> returns:
> 
> a, b, c
> a, b, d
> a, b, e
> ...
> 
> etc.
> 
> Many thanks in advance,
> 
> - Yuelin Li.
> 
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
> -.-.-.-.-.-.-.-.-
> 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
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._.
> _._._._._._._._._
> 


LEGAL NOTICE
Unless expressly stated otherwise, this message is confidential and may be privileged. It is intended for the addressee(s) only. Access to this E-mail by anyone else is unauthorized. If you are not an addressee, any disclosure or copying of the contents of this E-mail or any action taken (or not taken) in reliance on it is unauthorized and may be unlawful. If you are not an addressee, please inform the sender immediately.
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list