[R] finding the minimum positive value of some data

Marc Schwartz marc_schwartz at comcast.net
Mon Sep 10 20:31:21 CEST 2007


On Mon, 2007-09-10 at 11:20 -0700, dxc13 wrote:
> useRs,
> 
> I am looking to find the minimum positive value of some data I have. 
> Currently, I am able to find the minimum of data after I apply some other
> functions to it:
> 
> > x
>  [1]  1  0  1  2  3  3  4  5  5  5  6  7  8  8  9  9 10 10
> 
> > sort(x)
>  [1]  0  1  1  2  3  3  4  5  5  5  6  7  8  8  9  9 10 10
> 
> > diff(sort(x))
>  [1] 1 0 1 1 0 1 1 0 0 1 1 1 0 1 0 1 0
> 
> > min(diff(sort(x)))
> [1] 0
> 
> The minimum is given as zero, which is clearly true, but I am interested in
> only the positive minimum, which is 1.  Can I find this by using only 1 line
> of code, like I have above? Thanks!
> 
> dxc13

It's not clear to me which vector you wish to get the minimum for, but
the basic premise would be along the lines of:

> x
 [1]  1  0  1  2  3  3  4  5  5  5  6  7  8  8  9  9 10 10


> min(x[which(x > 0)])
[1] 1

or

> min(which(diff(sort(x)) > 0))
[1] 1

See ?which

HTH,

Marc Schwartz



More information about the R-help mailing list