[R] binary order combinations

Dimitri Liakhovitski ld7631 at gmail.com
Fri Sep 5 18:15:56 CEST 2008


Henrik,
this is amazing! wow!
Thank you so much!
Dimitri

On 9/5/08, Henrik Bengtsson <hb at stat.berkeley.edu> wrote:
> names <- sprintf("V%d", 1:4);
> n <- length(names);
> stopifnot(n <= 32); # Theoretical upper limit
> x <- matrix(intToBits(1:(2^n-1)), ncol=2^n-1);
> x <- x[1:n,,drop=FALSE];
> keys <- apply(x, MARGIN=2, FUN=function(z) paste(names[as.logical(z)],
> collapse=":"));
> print(keys);
>
>  [1] "V1"          "V2"          "V1:V2"       "V3"
>  [5] "V1:V3"       "V2:V3"       "V1:V2:V3"    "V4"
>  [9] "V1:V4"       "V2:V4"       "V1:V2:V4"    "V3:V4"
> [13] "V1:V3:V4"    "V2:V3:V4"    "V1:V2:V3:V4"
>
> /H
>
> On Fri, Sep 5, 2008 at 8:23 AM, Dimitri Liakhovitski <ld7631 at gmail.com> wrote:
> > I am not sure it can do it. Besides, I ran a test of combos from quantreg:
> >
> > library(quantreg)
> > H<-1:3
> > test.combos<-lapply(1:3,function(x)
> > {combn(H,x)
> > })
> >
> > Every time I tried it crashed my R...
> >
> > :(
> >
> > Dimitri
> >
> > On 9/5/08, roger koenker <rkoenker at uiuc.edu> wrote:
> >> Does ?combos in the quantreg package do what you want?
> >>
> >>
> >> url:    www.econ.uiuc.edu/~roger            Roger Koenker
> >> email    rkoenker at uiuc.edu            Department of Economics
> >> vox:     217-333-4558                University of Illinois
> >> fax:       217-244-6678                Champaign, IL 61820
> >>
> >>
> >>
> >>
> >> On Sep 5, 2008, at 9:58 AM, Dimitri Liakhovitski wrote:
> >>
> >> >
> >> > Dear all!
> >> >
> >> > I have a vector of names
> >> > names<-("V1", "V2", "V3",....., "V15")
> >> >
> >> > I could create all possible combinations of these names (of all
> >> > lengths) using R:
> >> >
> >> > combos<-lapply(1:15,function(x)
> >> > {combn(names,x)
> >> > })
> >> >
> >> > I get a list with all possible combinations of elements of 'names'
> >> > that looks like this (just the very beginning of it):
> >> >
> >> > [[1]] - the first element contains all combinations of 1 name
> >> >    [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13]
> >> [,14]
> >> > [1,] "V1" "V2" "V3" "V4" "V5" "V6" "V7" "V8" "V9" "V10" "V11" "V12" "V13"
> >> "V14"
> >> >    [,15]
> >> > [1,] "V15"
> >> >
> >> > [[2]] - the second element contains all possible combinations of 2 names
> >> >    [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]  [,10] [,11] [,12] [,13]
> >> > [1,] "V1" "V1" "V1" "V1" "V1" "V1" "V1" "V1" "V1"  "V1"  "V1"  "V1"  "V1"
> >> > [2,] "V2" "V3" "V4" "V5" "V6" "V7" "V8" "V9" "V10" "V11" "V12" "V13" "V14"
> >> > .
> >> > .
> >> > .
> >> > etc.
> >> >
> >> > My question is: Is there any way to re-arrange all sub-elements of the
> >> > above list (i.e., all possible combinations of names such as V1,
> >> > V1:V3, V1:V2:V4:V5) in a binary system order. More specifically,
> >> > according to this system:
> >> > V1=1
> >> > V2=2
> >> > V3=4
> >> > V4=8
> >> > V5=16, etc....
> >> >
> >> > So, I'd like those combinations to be arranged in a vector in the
> >> > following order:
> >> > 1. V1 (because V1=1)
> >> > 2. V2 (because V2=2)
> >> > 3. V1:V2 (because V1=1 and V2=2 so that 1+2=3)
> >> > 4. V3 (because V3=4)
> >> > 5. V1:V3 (because V1=1 and V3=4 so that 1+4=5)
> >> > 6. V2:V3 (because V2=2 and V3=4 so that 2+4=6)
> >> > 7. V1:V2:V3 (because V1=1 and V2=2 and V3=4 so that 1+2+4=7)
> >> > 8. V4 (because V4=8)
> >> > etc.
> >> >
> >> > Is it at all possible?
> >> > Or maybe there is a way to create the name combinations in such an
> >> > order in the first place?
> >> >
> >> > Thank you very much!
> >> > Dimitri Liakhovitski
> >> >
> >> > ______________________________________________
> >> > 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.
> >> >
> >>
> >>
> >
> > ______________________________________________
> > 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.
> >
>



More information about the R-help mailing list