[R] how to divide each column in a matrix by its colSums?

2010-11-28 Thread casperyc
Hi, I have a matrix, say m=matrix(c( 983,679,134, 383,416,84, 2892,2625,570 ),nrow=3 ) i can find its row/col sum by rowSums(m) colSums(m) How do I divide each row/column by its rowSum/colSums and still return in the matrix form? (i.e. the new rowSums/colSums

Re: [R] how to divide each column in a matrix by its colSums?

2010-11-28 Thread Jorge Ivan Velez
Hi Casper, Try m/colSums(m) m/rowSums(m) HTH, Jorge On Sun, Nov 28, 2010 at 3:46 PM, casperyc wrote: Hi, I have a matrix, say m=matrix(c( 983,679,134, 383,416,84, 2892,2625,570 ),nrow=3 ) i can find its row/col sum by rowSums(m) colSums(m) How do

Re: [R] how to divide each column in a matrix by its colSums?

2010-11-28 Thread casperyc
In that case, there are values 1, which is clearly not what I wanted. Thanks. I think I should use prop.table -- View this message in context: http://r.789695.n4.nabble.com/how-to-divide-each-column-in-a-matrix-by-its-colSums-tp3062739p3062752.html Sent from the R help mailing list archive

Re: [R] how to divide each column in a matrix by its colSums?

2010-11-28 Thread Spencer Graves
prop.table divides every element by the matrix total, not its colSums: m - matrix(1:4, 2) m [,1] [,2] [1,]13 [2,]24 prop.table(m) [,1] [,2] [1,] 0.1 0.3 [2,] 0.2 0.4 m/rowSums(m) divides every row by its rowSum: m/rowSums(m) [,1] [,2]

Re: [R] how to divide each column in a matrix by its colSums?

2010-11-28 Thread casperyc
I am using prop.table(m,1) and prop.table(m,2) my aim. which I think is the most 'easy' way. Thanks. Casper -- View this message in context: http://r.789695.n4.nabble.com/how-to-divide-each-column-in-a-matrix-by-its-colSums-tp3062739p3062797.html Sent from the R help mailing list archive

Re: [R] how to divide each column in a matrix by its colSums?

2010-11-28 Thread Erich Neuwirth
sweep(m,1,rowSums(m),/) sweep(m,2,colSums(m),/) On 11/28/2010 9:55 PM, casperyc wrote: In that case, there are values 1, which is clearly not what I wanted. Thanks. I think I should use prop.table __ R-help@r-project.org mailing list

Re: [R] how to divide each column in a matrix by its colSums?

2010-11-28 Thread Spencer Graves
Thanks: I didn't read the prop.table help page with sufficient care. On 11/28/2010 1:48 PM, casperyc wrote: I am using prop.table(m,1) and prop.table(m,2) my aim. which I think is the most 'easy' way. Thanks. Casper -- Spencer Graves, PE, PhD President and Chief Operating Officer