[R] finding the next highest number in an array

Dimitris Rizopoulos d.rizopoulos at erasmusmc.nl
Wed Jul 28 13:28:47 CEST 2010


a couple of the many possible ways are:

x <- c(23,36,45,62,79,103,109)
thr <- 67

x[x > thr][1]
head(x[x > thr], 1)


I hope it helps.

Best,
Dimitris


On 7/28/2010 12:12 PM, Raghu wrote:
> Hi
>
> I have a sorted array ( in ascending order) and I want to find the subscript
> of a number in the array which is the the next highest number to a given
> number. For example,if I have 67 as a given number and if I have a vector
> x=c(23,36,45,62,79,103,109), then how do I get the subscript 5 from x (to
> get 79 which is the next highest to 67) without using a for loop?
>
> Thx
>

-- 
Dimitris Rizopoulos
Assistant Professor
Department of Biostatistics
Erasmus University Medical Center

Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands
Tel: +31/(0)10/7043478
Fax: +31/(0)10/7043014



More information about the R-help mailing list