[R] A vectorization question

2007-01-09 Thread Christos Hatzis
Hi, A function calculates the absolute difference between the two largest values of each row of a matrix, as shown in the following example code: cx - matrix(runif(15),5) cy - t( apply(cx, 1, order, decreasing=TRUE) ) cz - rep(0, nrow(cx)) for( i in 1:nrow(cx) ) cz[i] - abs(diff(cx[i,

Re: [R] A vectorization question

2007-01-09 Thread Marc Schwartz
On Tue, 2007-01-09 at 16:10 -0500, Christos Hatzis wrote: Hi, A function calculates the absolute difference between the two largest values of each row of a matrix, as shown in the following example code: cx - matrix(runif(15),5) cy - t( apply(cx, 1, order, decreasing=TRUE) ) cz -

Re: [R] A vectorization question

2007-01-09 Thread Christos Hatzis
Thanks, Marc. This is what I was trying to do but could not get it to work. -Christos -Original Message- From: Marc Schwartz [mailto:[EMAIL PROTECTED] Sent: Tuesday, January 09, 2007 4:32 PM To: [EMAIL PROTECTED] Cc: 'R-help' Subject: Re: [R] A vectorization question On Tue, 2007-01

Re: [R] A vectorization question

2007-01-09 Thread Marc Schwartz
Welcome Christos. Note that my first example can actually be simplified to: apply(mat, 1, function(x) -diff(sort(x, decreasing = TRUE)[1:2])) Since we really just need to negate the difference, rather than take the abs(). The advantage of this approach is that the two max values will always

Re: [R] A vectorization question

2007-01-09 Thread Christos Hatzis
again. -Christos -Original Message- From: Marc Schwartz [mailto:[EMAIL PROTECTED] Sent: Tuesday, January 09, 2007 5:07 PM To: [EMAIL PROTECTED] Cc: 'R-help' Subject: RE: [R] A vectorization question Welcome Christos. Note that my first example can actually be simplified to: apply(mat