[R] Trying to avoid a loop

Thomas Lumley thomas at biostat.washington.edu
Wed Jan 24 00:05:10 CET 2001


On Tue, 23 Jan 2001, Michael Roberts wrote:

> 
> Wow.  That was a quick reply. Thanks...  but see below.
> 
> Michael J. Roberts
> Resource Economics Division, PMT
> USDA-ERS
> 202-694-5557
> 
> >>> Thomas Lumley <thomas at biostat.washington.edu> 01/23 5:51 PM >>>
> On Tue, 23 Jan 2001, Michael Roberts wrote:
> 
> > Hello:
> > 
> > Although I could write a loop to do the following, I would like
> > to avoid this as it would be expensive.  Does anyone know if
> > there is an R function that would make this faster?
> > 
> > Given a 3 dimensional array, say Z with dimensions {n,l,k},  I
> would
> > like to create a matrix Y with dimensions {n,l}, each element of
> which
> > contains the position {k} with the largest element that dimension.
> > 
> 
> apply(z,c(1,2),max)
> 
> This is close, but not exactly what I am looking for.  I don't want
> the maximum value, but the position of the maximum value.  Is there
> something like an "argmax" I could put in for max?

apply(z,1:2, function(x) which(x==max(x)))

	-thomas



-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list