Re: [R] A vectorization question

From: Marc Schwartz <marc_schwartz_at_comcast.net>
Date: Tue 09 Jan 2007 - 22:07:22 GMT

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 09:11:58 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 - 05:30:26 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.