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. Many thanks, Michael Roberts -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
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) -thomas Thomas Lumley Asst. Professor, Biostatistics tlumley at u.washington.edu University of Washington, Seattle -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
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}, Iwould> like to create a matrix Y with dimensions {n,l}, each element ofwhich> 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? Thanks a lot -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Date: Tue, 23 Jan 2001 17:37:31 -0500 From: Michael Roberts <mroberts at ers.usda.gov> X-Mailer: Mozilla 4.5 [en] (WinNT; U) X-Accept-Language: en MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Sender: owner-r-help at stat.math.ethz.ch Precedence: bulk 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. Many thanks, Michael Roberts -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._ apply(z,c(1,2),function(x) rev(order(x))) -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
>>>>> "TL" == Thomas Lumley <thomas at biostat.washington.edu> writes:TL> 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? TL> apply(z,1:2, function(x) which(x==max(x))) and exactly because the above is not quite efficient, I had added the which.max (and which.min) ~~~~~~~~~ ~~~~~~~~~ functions to R (1.1.0). I.e. apply(z,1:2, which.max) Note further that we have the (original MASS) function max.col to work for matrices which again is more efficient than apply(mat, 1, which.max) {and not quite equivalent: max.col breaks ties at random, which.max always gives the first max}. Now, you can reshape your rank-3 array {n,l,k} into a matrix {n*l, k} use max.col() on that matrix and reshape the {n * l} vector to the desired {n, l} matrix: x <- rnorm(50000) ar <- array(x, dim = c(25,40,50)) system.time(m1 <- apply(ar, 1:2, which.max)) system.time({ mat <- ar dim(mat) <- c(25*40, 50) m2 <- matrix(max.col(mat), 25, 40) }) all(m1 == m2) # these two are identical ## On my Linux machine, the ## max.col() version uses 0.03 sec compared to 0.10 for the apply() solution -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._