On 23/08/2017 6:25 PM, Bert Gunter wrote:> Doesn't sort by size of subgroup. I interpret the phrase I asterisked as:You were fooled by Peter's tricky single negative. Duncan Murdoch> > Your code does the following: > > First subsets of size 1 are given. > Then all subsets of size 2. > Then all subsets of size 3. > etc. > > Your code does not do this (quite). > > If you meant something else, then please clarify. > > Cheers, > Bert > > > Bert Gunter > > "The trouble with having an open mind is that people keep coming along > and sticking things into it." > -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) > > > On Wed, Aug 23, 2017 at 3:14 PM, peter dalgaard <pdalgd at gmail.com> wrote: >> >>> On 23 Aug 2017, at 23:12 , Bert Gunter <bgunter.4567 at gmail.com> wrote: >>> >>>> This points to a different algorithm where you write 0:(2^n-1) as n-digit binary numbers and chose items corresponding to the 1s. That won't give the combinations **sorted by size of selected subgroup** though. Something like this: >>> >>> No it doesn't. >>> -- Bert >> >> Doesn't what? Do what I say it won't?? >> >> -pd >> >> -- >> Peter Dalgaard, Professor, >> Center for Statistics, Copenhagen Business School >> Solbjerg Plads 3, 2000 Frederiksberg, Denmark >> Phone: (+45)38153501 >> Office: A 4.23 >> Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com >> >> >> >> >> >> >> >> >> > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. >
> On 24 Aug 2017, at 01:25 , Duncan Murdoch <murdoch.duncan at gmail.com> wrote: > > On 23/08/2017 6:25 PM, Bert Gunter wrote: >> Doesn't sort by size of subgroup. I interpret the phrase I asterisked as: > > You were fooled by Peter's tricky single negative. ><giggles>... Let's do this more carefully, then: M <- as.matrix(do.call(expand.grid, rep(list(0:1),5))) M # This is just 0:31 encoded as (little-endian) binary apply(M, 1, function(i) sum((2^(0:4))[i])) # now, use rows of M for logical indexing into "A"-"E" mode(M) <- "logical" (l <- apply(M,1,function(i)LETTERS[1:5][i])) # Clearly not sorted by group size: #[[1]] #character(0) # #[[2]] #[1] "A" # #[[3]] #[1] "B" # #[[4]] #[1] "A" "B" # #[[5]] #[1] "C" # requires this to sort: o <- order(sapply(l, length)) l[o] -- Peter Dalgaard, Professor, Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Office: A 4.23 Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com
> On 24 Aug 2017, at 11:58 , peter dalgaard <pdalgd at gmail.com> wrote: > > apply(M, 1, function(i) sum((2^(0:4))[i]))-- Peter Dalgaard, Professor, Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Office: A 4.23 Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com
> On 24 Aug 2017, at 11:58 , peter dalgaard <pdalgd at gmail.com> wrote: > > M <- as.matrix(do.call(expand.grid, rep(list(0:1),5))) > M > # This is just 0:31 encoded as (little-endian) binary > apply(M, 1, function(i) sum((2^(0:4))[i])) > > # now, use rows of M for logical indexing into "A"-"E" > mode(M) <- "logical"Whoops. That apply() line should either come _after_ conversion to logical, or read apply(M, 1, function(i) sum(2^(0:4) * i)) As in:> M <- as.matrix(do.call(expand.grid, rep(list(0:1),5))) > apply(M, 1, function(i) sum(2^(0:4) * i))[1] 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 [26] 25 26 27 28 29 30 31 -- Peter Dalgaard, Professor, Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Office: A 4.23 Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com