[R] binary order combinations

Dimitri Liakhovitski ld7631 at gmail.com
Fri Sep 5 16:58:42 CEST 2008


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



More information about the R-help mailing list