Re: [R] dividing a matrix by positive sum or negative sum depending on the sign

2009-11-12 Thread Hao Cen
Hi David and Dimitris, Thanks for your suggestions. They are very helpful. Jeff On Wed, November 11, 2009 12:12 pm, David Winsemius wrote: > > On Nov 11, 2009, at 10:57 AM, David Winsemius wrote: > > >> >> On Nov 11, 2009, at 10:36 AM, Dimitris Rizopoulos wrote: >> >> >>> one approach is the fo

Re: [R] dividing a matrix by positive sum or negative sum depending on the sign

2009-11-11 Thread David Winsemius
On Nov 11, 2009, at 10:57 AM, David Winsemius wrote: On Nov 11, 2009, at 10:36 AM, Dimitris Rizopoulos wrote: one approach is the following: mat <- rbind(c(-1, -1, 2, NA), c(3, 3, -2, -1), c(1, 1, NA, -2)) mat / ave(abs(mat), row(mat), sign(mat), FUN = sum) Very elegant. My solution was

Re: [R] dividing a matrix by positive sum or negative sum depending on the sign

2009-11-11 Thread David Winsemius
On Nov 11, 2009, at 10:36 AM, Dimitris Rizopoulos wrote: one approach is the following: mat <- rbind(c(-1, -1, 2, NA), c(3, 3, -2, -1), c(1, 1, NA, -2)) mat / ave(abs(mat), row(mat), sign(mat), FUN = sum) Very elegant. My solution was a bit more pedestrian, but may have some speed advanta

Re: [R] dividing a matrix by positive sum or negative sum depending on the sign

2009-11-11 Thread Dimitris Rizopoulos
one approach is the following: mat <- rbind(c(-1, -1, 2, NA), c(3, 3, -2, -1), c(1, 1, NA, -2)) mat / ave(abs(mat), row(mat), sign(mat), FUN = sum) I hope it helps. Best, Dimitris Hao Cen wrote: Hi, I have a matrix with positive numbers, negative numbers, and NAs. An example of the matrix

[R] dividing a matrix by positive sum or negative sum depending on the sign

2009-11-11 Thread Hao Cen
Hi, I have a matrix with positive numbers, negative numbers, and NAs. An example of the matrix is as follows -1 -1 2 NA 3 3 -2 -1 1 1 NA -2 I need to compute a scaled version of this matrix. The scaling method is dividing each positive numbers in each row by the sum of positive numbers in that r