[R] Sorting rows of a binary matrix

Charles C. Berry cberry at tajo.ucsd.edu
Thu Feb 22 18:56:27 CET 2007


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



More information about the R-help mailing list