Re: [R] A vectorization question

From: Christos Hatzis <christos_at_nuverabio.com>
Date: Tue 09 Jan 2007 - 22:24:12 GMT

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@comcast.net] Sent: Tuesday, January 09, 2007 5:07 PM
To: christos@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>



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Wed Jan 10 15:45:34 2007

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Wed 10 Jan 2007 - 08:30:25 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.