[R] A vectorization question
Marc Schwartz
marc_schwartz at comcast.net
Tue Jan 9 23:07:22 CET 2007
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>
More information about the R-help
mailing list