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
|