s-news
[Top] [All Lists]

RE: [S] combinations

To: "S-NEWS" <s-news@utstat.toronto.edu>
Subject: RE: [S] combinations
From: "Ed Callahan" <callahan@envstat.com>
Date: Sun, 26 Apr 1998 16:55:55 -0500
Importance: Normal
In-reply-to: <000001bd707b$717c45e0$160fa3d1@envstat>
Sender: owner-s-news@wubios.wustl.edu
I quickly got advice from several people in private postings, including
Gabriel Escarela, Colin Mallows, Bill Venables and Roger Koenker.  Dr.
Venables solution has worked perfectly for me:

> subsets <- function(n, k, set = 1:n)
    if(k <= 0) NULL else
      if(k >= n) set else
        rbind(cbind(set[1], Recall(n - 1, k - 1, set[-1])),
        Recall(n - 1, k, set[-1]))

It's a neat example of recursion.  Thanks to all who helped.

Ed

----------------------------------
  Edward Callahan
  Environmental Statistics

  callahan@envstat.com
  http://www.envstat.com
----------------------------------


-----Original Message-----
I need a function that returns a matrix of all the combinations of size k
from n items.  So with n=4 and k=2 I would want

1 2
1 3
1 4
2 3
2 4
3 4

Does anyone know how to achieve this in S-Plus 4.0?

Thanks
Ed

-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu.  To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message:  unsubscribe s-news

<Prev in Thread] Current Thread [Next in Thread>