Hallo, The command: x <- 3 mat <- as.matrix(expand.grid(rep(list(0:1), x))) generates a matrix with 2^x columns containing the binary representations of the decimals from 0 to (2^x-1), here from 0 to 7. But the rows are not sorted in this order. How can sort the rows the ascending order of the decimals they represent, preferably without a function which converts binaries to decimals (which I have)? Alternatively, generate a matrix that has the rows sorted that way? Thanks, Serguei [[alternative HTML version deleted]]
I'm sure there are more elegant ways, but this should work:> ix<-order(mat[,1],mat[,2],mat[,3]) > ix[1] 1 5 3 7 2 6 4 8> mat[ix,]Var1 Var2 Var3 1 0 0 0 5 0 0 1 3 0 1 0 7 0 1 1 2 1 0 0 6 1 0 1 4 1 1 0 8 1 1 1 On 22/02/07, Serguei Kaniovski <Serguei.Kaniovski at wifo.ac.at> wrote:> > Hallo, > > The command: > > x <- 3 > mat <- as.matrix(expand.grid(rep(list(0:1), x))) > > generates a matrix with 2^x columns containing the binary representations > of the decimals from 0 to (2^x-1), here from 0 to 7. But the rows are not > sorted in this order. > > How can sort the rows the ascending order of the decimals they represent, > preferably without a function which converts binaries to decimals (which I > have)? Alternatively, generate a matrix that has the rows sorted that way? > > Thanks, > Serguei > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch 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 Barron Said Business School University of Oxford Park End Street Oxford OX1 1HP
Hello Serguei,
Is this what you need?
myfunc <- function(x) {
create <- function(idx) {
rep.int(c(rep.int(0,2^(idx-1)), rep.int(1,2^(idx-1))),
2^x/2^idx)
}
sapply(rev(seq(x)), create)
}
> myfunc(3)
[,1] [,2] [,3]
[1,] 0 0 0
[2,] 0 0 1
[3,] 0 1 0
[4,] 0 1 1
[5,] 1 0 0
[6,] 1 0 1
[7,] 1 1 0
[8,] 1 1 1
For numerical values only, this is faster than expand.grid().
Alternatively (for multiple values in separate varaibles), you could use the
function createMatrix() in package QCA.
HTH,
Adrian
On Thursday 22 February 2007 12:50, Serguei Kaniovski
wrote:> Hallo,
>
> The command:
>
> x <- 3
> mat <- as.matrix(expand.grid(rep(list(0:1), x)))
>
> generates a matrix with 2^x columns containing the binary representations
> of the decimals from 0 to (2^x-1), here from 0 to 7. But the rows are not
> sorted in this order.
>
> How can sort the rows the ascending order of the decimals they represent,
> preferably without a function which converts binaries to decimals (which I
> have)? Alternatively, generate a matrix that has the rows sorted that way?
>
> Thanks,
> Serguei
> [[alternative HTML version deleted]]
--
Adrian Dusa
Romanian Social Data Archive
1, Schitu Magureanu Bd
050025 Bucharest sector 5
Romania
Tel./Fax: +40 21 3126618 \
+40 21 3120210 / int.101
And, for multiple bases:
myfunc <- function(cols, bases) {
create <- function(idx) {
rep.int(c(sapply(seq_len(bases)-1, function(x)
rep.int(x, bases^(idx-1)))), bases^cols/bases^idx)
}
sapply(rev(seq_len(cols)), create)
}
# For 3 columns in base 2
myfunc(3, 2)
# For 3 columns in base 3
myfunc(3, 3)
hth,
Adrian
On Thursday 22 February 2007 15:00, Adrian Dusa wrote:> Hello Serguei,
>
> Is this what you need?
>
> myfunc <- function(x) {
> create <- function(idx) {
> rep.int(c(rep.int(0,2^(idx-1)), rep.int(1,2^(idx-1))),
> 2^x/2^idx)
> }
> sapply(rev(seq(x)), create)
> }
>
> > myfunc(3)
>
> [,1] [,2] [,3]
> [1,] 0 0 0
> [2,] 0 0 1
> [3,] 0 1 0
> [4,] 0 1 1
> [5,] 1 0 0
> [6,] 1 0 1
> [7,] 1 1 0
> [8,] 1 1 1
>
> For numerical values only, this is faster than expand.grid().
> Alternatively (for multiple values in separate varaibles), you could use
> the function createMatrix() in package QCA.
>
> HTH,
> Adrian
>
> On Thursday 22 February 2007 12:50, Serguei Kaniovski wrote:
> > Hallo,
> >
> > The command:
> >
> > x <- 3
> > mat <- as.matrix(expand.grid(rep(list(0:1), x)))
> >
> > generates a matrix with 2^x columns containing the binary
representations
> > of the decimals from 0 to (2^x-1), here from 0 to 7. But the rows are
not
> > sorted in this order.
> >
> > How can sort the rows the ascending order of the decimals they
represent,
> > preferably without a function which converts binaries to decimals
(which
> > I have)? Alternatively, generate a matrix that has the rows sorted
that
> > way?
> >
> > Thanks,
> > Serguei
> > [[alternative HTML version deleted]]
--
Adrian Dusa
Romanian Social Data Archive
1, Schitu Magureanu Bd
050025 Bucharest sector 5
Romania
Tel./Fax: +40 21 3126618 \
+40 21 3120210 / int.101
On Thu, 22 Feb 2007, Serguei Kaniovski wrote:> > Hallo, > > The command: > > x <- 3 > mat <- as.matrix(expand.grid(rep(list(0:1), x))) > > generates a matrix with 2^x columns containing the binary representations > of the decimals from 0 to (2^x-1), here from 0 to 7. But the rows are not > sorted in this order. > > How can sort the rows the ascending order of the decimals they represent, > preferably without a function which converts binaries to decimals (which I > have)? Alternatively, generate a matrix that has the rows sorted that way?The alternative: mat <- as.matrix(expand.grid(rep(list(0:1), x))[ , x:1 ] )> > Thanks, > Serguei > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >Charles C. Berry (858) 534-2098 Dept of Family/Preventive Medicine E mailto:cberry at tajo.ucsd.edu UC San Diego http://biostat.ucsd.edu/~cberry/ La Jolla, San Diego 92093-0901