Dale Steele
2009-Mar-13 12:35 UTC
[R] code to find all distinct subsets of size r from a set of size n
I'm doing a permutation test and need to efficiently generate all distinct subsets of size r from a set of size n. P 138 of MASS (4th ed) notes that "The code to generate this efficiently is in the scripts". I was unable to find this code on quick inspection of the \library\MASS\scripts file for Chapter 5 and 'subsets' is not a function in MASS. I did find this problem is discussed in RNews, Programmer's Niche 1(1):27 - 30 and RNews, 1(2):29-31. Is there function in 'base' R or other package to do this? Thanks. --Dale
Kingsford Jones
2009-Mar-13 13:25 UTC
[R] code to find all distinct subsets of size r from a set of size n
For permutations a couple of options are 'permutations' in package gtools, and 'urnsamples' in package prob hth, Kingsford Jones On Fri, Mar 13, 2009 at 6:35 AM, Dale Steele <dale.w.steele at gmail.com> wrote:> I'm doing a permutation test and need to efficiently generate all > distinct subsets of size r from a set of size n. ?P 138 of MASS (4th > ed) notes that ?"The code to generate this efficiently is in the > scripts". ?I was unable to find this code on quick inspection of the > \library\MASS\scripts file for Chapter 5 and 'subsets' is not a > function in MASS. > > I did find this problem is discussed in RNews, Programmer's Niche > 1(1):27 - 30 and RNews, 1(2):29-31. ?Is there function in 'base' ?R or > other package to do this? ?Thanks. ?--Dale > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
David Winsemius
2009-Mar-13 14:50 UTC
[R] code to find all distinct subsets of size r from a set of size n
If your subsets are to be taken from the rows of a dataframe, df, and the size is r, then something like this could satisfy: apply(combn(nrow(df), r), 2, function(x) df[x, ]) (Although these are not really permutations as I understand that term.) -- David Winsemius On Mar 13, 2009, at 8:35 AM, Dale Steele wrote:> I'm doing a permutation test and need to efficiently generate all > distinct subsets of size r from a set of size n. P 138 of MASS (4th > ed) notes that "The code to generate this efficiently is in the > scripts". I was unable to find this code on quick inspection of the > \library\MASS\scripts file for Chapter 5 and 'subsets' is not a > function in MASS. > > I did find this problem is discussed in RNews, Programmer's Niche > 1(1):27 - 30 and RNews, 1(2):29-31. Is there function in 'base' R or > other package to do this? Thanks. --Dale > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.David Winsemius, MD Heritage Laboratories West Hartford, CT