# [R] A vectorization question

Christos Hatzis christos at nuverabio.com
Tue Jan 9 23:24:12 CET 2007

```That's true.  Just need to negate the difference.  Actually, straight diff
can be used after reversing the vector:

apply(mat, 1, function(x) diff(sort(x, decreasing = TRUE)[2:1]))

I only have 3 columns in my matrix so sorting should not add much overhead,
but I will time both versions.

Thanks again.
-Christos

-----Original Message-----
From: Marc Schwartz [mailto:marc_schwartz at comcast.net]
Sent: Tuesday, January 09, 2007 5:07 PM
To: christos at nuverabio.com
Cc: 'R-help'
Subject: RE: [R] A vectorization question

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 be the
first and second values, so will be independent of the length of 'x' (number
of columns in the matrix).

Using the second example more generally, you would have to use something
like:

apply(mat, 1, function(x) diff(sort(x)[-c(1:(length(x) - 2))]))

in the subsetting of the sort() results or precalcuate the indices (ie.
ncol(mat) and ncol(mat) - 1).

Might add a bit more overhead, but testing would give you more empiric
timing data. That might have to be balanced by whether the rows tend to be
random in order or closer to being sorted in increasing/decreasing order,
which would affect the sort time. Worst case scenario is generally having to
reverse the sort order. Of course, if the matrices are "relatively" small,
sorting time would likely be a non-issue.

HTH,

Marc

On Tue, 2007-01-09 at 16:39 -0500, Christos Hatzis wrote:
> Thanks, Marc.
> This is what I was trying to do but could not get it to work.
>
> -Christos

<snip>

```