Hello, I have two vectors x1 and x2 both in increasing order. I want to select the x1[j]th entry which is the max min of the x2[i]th entry. I can do this using if and for statements but is there a quick way to do it without running a loop? Thank you in advance, Pantelis
On Fri, Dec 06, 2002 at 01:12:27PM -0400, Pantelis Andreou wrote:> I have two vectors x1 and x2 both in increasing order. > I want to select the x1[j]th entry which is the max min of the x2[i]th > entry. I can do this using if and for statements but is there a quick way > to do it without running a loop?Use `which' function: x1[which(x2 == max(x2))] WBR, Timur.
Hello again, I think I made a mistake discribing the problem. I have two column vectors x1 and x2 both in increasing order. I need a column vector x3 such that the jth entry of x3 is the maximum ith entry x1 which is smaller than the jth entry of x2. Regards, Pantelis
Example would help! On Fri, 6 Dec 2002, Pantelis Andreou wrote:> Date: Fri, 6 Dec 2002 14:09:22 -0400 (AST) > From: Pantelis Andreou <pan at mathstat.dal.ca> > To: r-help at stat.math.ethz.ch > Subject: [R] fast code > > Hello again, > I think I made a mistake discribing the problem. > I have two column vectors x1 and x2 both in increasing order. > I need a column vector x3 such that > the jth entry of x3 is the maximum ith entry x1 which is smaller > than the jth entry of x2. > Regards, > Pantelis > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > http://www.stat.math.ethz.ch/mailman/listinfo/r-help >
Hello again, here is an example x1 x2 x3 3 3.1 3 4 4.2 4 6.5 5 4 20 24 20 35 38 35 On Fri, 6 Dec 2002, Janusz Kawczak wrote:> Example would help! > > On Fri, 6 Dec 2002, Pantelis Andreou wrote: > > > Date: Fri, 6 Dec 2002 14:09:22 -0400 (AST) > > From: Pantelis Andreou <pan at mathstat.dal.ca> > > To: r-help at stat.math.ethz.ch > > Subject: [R] fast code > > > > Hello again, > > I think I made a mistake discribing the problem. > > I have two column vectors x1 and x2 both in increasing order. > > I need a column vector x3 such that > > the jth entry of x3 is the maximum ith entry x1 which is smaller > > than the jth entry of x2. > > Regards, > > Pantelis > > > > ______________________________________________ > > R-help at stat.math.ethz.ch mailing list > > http://www.stat.math.ethz.ch/mailman/listinfo/r-help > > > >
Perhaps this will do the trick:> tmp <- data.frame(x1=c(3,4,6.5,20,35), x2=c(3.1,4.2,5,24,38) ) > attach(tmp) > > tmp$x3 <- sapply( x2, function(val) max(x1[x1<val]) ) > > tmpx1 x2 x3 1 3.0 3.1 3 2 4.0 4.2 4 3 6.5 5.0 4 4 20.0 24.0 20 5 35.0 38.0 35 -G> -----Original Message----- > From: Pantelis Andreou [mailto:pan at mathstat.dal.ca] > Sent: Friday, December 06, 2002 1:21 PM > To: Janusz Kawczak > Cc: r-help at stat.math.ethz.ch > Subject: Re: [R] fast code > > > Hello again, > here is an example > x1 x2 x3 > 3 3.1 3 > 4 4.2 4 > 6.5 5 4 > 20 24 20 > 35 38 35 > > > On Fri, 6 Dec 2002, Janusz Kawczak wrote: > > > Example would help! > > > > On Fri, 6 Dec 2002, Pantelis Andreou wrote: > > > > > Date: Fri, 6 Dec 2002 14:09:22 -0400 (AST) > > > From: Pantelis Andreou <pan at mathstat.dal.ca> > > > To: r-help at stat.math.ethz.ch > > > Subject: [R] fast code > > > > > > Hello again, > > > I think I made a mistake discribing the problem. > > > I have two column vectors x1 and x2 both in increasing order. > > > I need a column vector x3 such that > > > the jth entry of x3 is the maximum ith entry x1 which is smaller > > > than the jth entry of x2. > > > Regards, > > > Pantelis > > > > > > ______________________________________________ > > > R-help at stat.math.ethz.ch mailing list > > > http://www.stat.math.ethz.ch/mailman/listinfo/r-help > > > > > > > > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > http://www.stat.math.ethz.ch/mailman/listinfo/r-help >LEGAL NOTICE\ Unless expressly stated otherwise, this message is ... [[dropped]]
You could use "cut" as follows: i <- cut(x2,c(x1,Inf),labels=FALSE) to get the intervals of x1 into which the entries of x2 fall, then x3 <- x1[i] to get the left endpoints. This will give NA for elements of x2 smaller than any element of x1, which I think you want. You may want to play with the options to "cut" to get the right behavior for exact equality between elements of x1 and x2. Note x2 need not be sorted. I don't see a way to take advantage of sorted x2 without explicit looping. Reid Huntsinger -----Original Message----- From: Pantelis Andreou [mailto:pan at mathstat.dal.ca] Sent: Friday, December 06, 2002 1:21 PM To: Janusz Kawczak Cc: r-help at stat.math.ethz.ch Subject: Re: [R] fast code Hello again, here is an example x1 x2 x3 3 3.1 3 4 4.2 4 6.5 5 4 20 24 20 35 38 35 On Fri, 6 Dec 2002, Janusz Kawczak wrote:> Example would help! > > On Fri, 6 Dec 2002, Pantelis Andreou wrote: > > > Date: Fri, 6 Dec 2002 14:09:22 -0400 (AST) > > From: Pantelis Andreou <pan at mathstat.dal.ca> > > To: r-help at stat.math.ethz.ch > > Subject: [R] fast code > > > > Hello again, > > I think I made a mistake discribing the problem. > > I have two column vectors x1 and x2 both in increasing order. > > I need a column vector x3 such that > > the jth entry of x3 is the maximum ith entry x1 which is smaller > > than the jth entry of x2. > > Regards, > > Pantelis > > > > ______________________________________________ > > R-help at stat.math.ethz.ch mailing list > > http://www.stat.math.ethz.ch/mailman/listinfo/r-help > > > >______________________________________________ R-help at stat.math.ethz.ch mailing list http://www.stat.math.ethz.ch/mailman/listinfo/r-help ------------------------------------------------------------------------------ Notice: This e-mail message, together with any attachments, contains information of Merck & Co., Inc. (Whitehouse Station, New Jersey, USA) that may be confidential, proprietary copyrighted and/or legally privileged, and is intended solely for the use of the individual or entity named in this message. If you are not the intended recipient, and have received this message in error, please immediately return this by e-mail and then delete it.