[R] Sorting rows of a binary matrix

2007-02-22 Thread Serguei Kaniovski

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@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.


Re: [R] Sorting rows of a binary matrix

2007-02-22 Thread David Barron
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
1000
5001
3010
7011
2100
6101
4110
8111


On 22/02/07, Serguei Kaniovski [EMAIL PROTECTED] 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@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

__
R-help@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.


Re: [R] Sorting rows of a binary matrix

2007-02-22 Thread Adrian Dusa
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,]000
[2,]001
[3,]010
[4,]011
[5,]100
[6,]101
[7,]110
[8,]111

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

__
R-help@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.


Re: [R] Sorting rows of a binary matrix

2007-02-22 Thread Adrian Dusa
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,]000
 [2,]001
 [3,]010
 [4,]011
 [5,]100
 [6,]101
 [7,]110
 [8,]111

 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

__
R-help@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.


Re: [R] Sorting rows of a binary matrix

2007-02-22 Thread Charles C. Berry
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@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:[EMAIL PROTECTED]   UC San Diego
http://biostat.ucsd.edu/~cberry/ La Jolla, San Diego 92093-0901

__
R-help@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.