All, Is there one liner way to obtain the max per observation for two vectors? I looked at apply and lapply but it seems that groundwork would have to be done before applying either of those. The code below does it but seems like overkill. Thanks! Dave x = rnorm(10) y = rnorm(10) ind = which(x < y) z = x z[ind] <- y[ind] ## z now contains the max's
On Thu, 28 Jun 2007 16:19:39 -0400, "Afshartous, David" <afshart at exchange.sba.miami.edu> wrote:> All,> Is there one liner way to obtain the max per observation for two > vectors? I looked at apply and lapply but it seems that groundwork > would have to be done before applying either of those. The code below > does it but seems like overkill.> Thanks! Dave> x = rnorm(10) y = rnorm(10)> ind = which(x < y) z = x z[ind] <- y[ind] ## z now contains the max's?pmax -- Seb
Please... use and **read** the docs: ?max ---> pmax Bert Gunter -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Afshartous, David Sent: Thursday, June 28, 2007 1:20 PM To: r-help at stat.math.ethz.ch Subject: [R] applying max elementwise to two vectors All, Is there one liner way to obtain the max per observation for two vectors? I looked at apply and lapply but it seems that groundwork would have to be done before applying either of those. The code below does it but seems like overkill. Thanks! Dave x = rnorm(10) y = rnorm(10) ind = which(x < y) z = x z[ind] <- y[ind] ## z now contains the max's ______________________________________________ R-help at 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.
Are you looking for pmax? (look at the help ?pmax and the examples and see if that does what you want). -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at intermountainmail.org (801) 408-8111> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of > Afshartous, David > Sent: Thursday, June 28, 2007 2:20 PM > To: r-help at stat.math.ethz.ch > Subject: [R] applying max elementwise to two vectors > > > > All, > > Is there one liner way to obtain the max per observation for > two vectors? > I looked at apply and lapply but it seems that groundwork > would have to be done before applying either of those. The > code below does it but seems like overkill. > > Thanks! > Dave > > x = rnorm(10) > y = rnorm(10) > > ind = which(x < y) > z = x > z[ind] <- y[ind] ## z now contains the max's > > ______________________________________________ > R-help at 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. >