Skip to content

binary order combinations

7 messages · Dimitri Liakhovitski, Roger Koenker, Henrik Bengtsson

#
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
#
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:

            
#
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:
#
You need to read the help file ?combos, and then use it to do indexing
of your objects, it only knows how to construct the integer combinations
given  a pair (n,p).

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 10:23 AM, Dimitri Liakhovitski 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:
#
Sorry,

I misread it first. I tried:

library(quantreg)
 test.combos<-lapply(1:15,function(x)
 {combos(15,x)
 })

It gives me a list with an order that is somewhat different from
before, but I am not sure it helps me much:

[[1]]
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14]
[1,]    1    2    3    4    5    6    7    8    9    10    11    12    13    14
     [,15]
[1,]    15

[[2]]
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14]
[1,]    1    1    1    1    1    1    1    1    1     1     1     1     1     1
[2,]    2   15   14   13   12   11   10    9    8     7     6     5     4     3
     [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23] [,24] [,25] [,26]
[1,]     2     2     2     2     2     2     2     2     2     2     2     2
[2,]     3    15    14    13    12    11    10     9     8     7     6     5


Dimitri
On 9/5/08, roger koenker <rkoenker at uiuc.edu> wrote:
#
Henrik,
this is amazing! wow!
Thank you so much!
Dimitri
On 9/5/08, Henrik Bengtsson <hb at stat.berkeley.edu> wrote: