[R] making matrix monotonous
Gabor Grothendieck
ggrothendieck at gmail.com
Wed Jun 7 13:13:12 CEST 2006
On 6/7/06, Romain Francois <francoisromain at free.fr> wrote:
> Le 07.06.2006 11:20, vincent at 7d4.com a écrit :
> > Spencer Graves a écrit :
> >
> >
> >> I agree it would be great to sort the variables in a correlation
> >> matrix to make it easier to read and see patterns. I don't know any
> >> functions for doing that. If it were my problem, I might "order" the
> >> variables by their first principal component. There may also be some
> >> cluster analysis way to do that, but I don't know it well enough to say.
> >> Hope this helps.
> >> Spencer Graves
> >>
> >
> > Thanks for your answer Spencer.
> >
> > Here is a first result of a very simple and naive approach.
> > http://7d4.com/r/
> >
> > Of course, there is no assumption the sorting is "optimal",
> > but on this little example it helps the matrix being
> > more readable.
> >
> > Vincent
> >
> Hello Vincent,
>
> Ahhh, the double for loop, the semicolon, the return call. you still
> believe in R code looking like C don't you.
> Try this one :
>
> matrix.sort2 <- function(M, fun = function(m) colSums(abs(m)) ){
> M[or <- order(fun(M) , decreasing=T), or]
> }
Even if this works I don't think its guaranteed since one cannot
be sure the first argument, or<-..., is evaluated before the second, or.
Also use TRUE in case there is a T variable in workspace:
matrix.sort3 <- function(M, fun = function(m) colSums(abs(m)) ) {
or <- order(fun(M), decreasing = TRUE)
M[or, or]
}
More information about the R-help
mailing list