[R] Arrange elements on a matrix according to rowSums + short 'apply' Q

2010-12-02 Thread Aaron Polhamus
Greetings, My goal is to create a Markov transition matrix (probability of moving from one state to another) with the 'highest traffic' portion of the matrix occupying the top-left section. Consider the following sample: inputData - c( c(5, 3, 1, 6, 7), c(9, 7, 3, 10, 11), c(1, 2, 3,

Re: [R] Arrange elements on a matrix according to rowSums + short 'apply' Q

2010-12-02 Thread Ivan Calandra
Hi, Here is a not so easy way to do your first step, but it works: MAT2 - cbind(MAT, rowSums(MAT)) MAT[order(MAT2[,6], decreasing=TRUE),] For the second, I don't know! HTH, Ivan Le 12/2/2010 09:46, Aaron Polhamus a écrit : Greetings, My goal is to create a Markov transition matrix

Re: [R] Arrange elements on a matrix according to rowSums + short 'apply' Q

2010-12-02 Thread Michael Bedward
Hi Aaron, Following up on Ivan's suggestion, if you want the column order to mirror the row order... mo - order(rowSums(MAT), decreasing=TRUE) MAT2 - MAT[mo, mo] Also, you don't need all those extra c() calls when creating inputData, just the outermost one. Regarding your second question, your

Re: [R] Arrange elements on a matrix according to rowSums + short 'apply' Q

2010-12-02 Thread Aaron Polhamus
Ivan and Michael, Many thanks for the tips, those solved my queries. Still interested in how to force custom functions to work over rows rather than columns when using apply, but the MAT/rowSums(MAT) technique is definitely the most efficient way to go for this application. Cheers, Aaron

Re: [R] Arrange elements on a matrix according to rowSums + short 'apply' Q

2010-12-02 Thread Petr Savicky
On Thu, Dec 02, 2010 at 01:13:40PM -0800, Aaron Polhamus wrote: ... Many thanks for the tips, those solved my queries. Still interested in how to force custom functions to work over rows rather than columns when using apply, In the command TMAT - apply(MAT, 1, function(X) X/sum(X)) the