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 -- View this message in context: http://www.nabble.com/finding-the-minimum-positive-value-of-some-data-tf4417250.html#a12599319 Sent from the R help mailing list archive at Nabble.com.
Henrique Dallazuanna
2007-Sep-10 18:26 UTC
[R] finding the minimum positive value of some data
Try this: min(diff(sort(x))[diff(sort(x))>0]) -- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O On 10/09/2007, dxc13 <dxc13@health.state.ny.us> 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 > -- > View this message in context: > http://www.nabble.com/finding-the-minimum-positive-value-of-some-data-tf4417250.html#a12599319 > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]
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! > > dxc13It'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
Gabor Grothendieck
2007-Sep-11 00:29 UTC
[R] finding the minimum positive value of some data
Here are some solutions each of which 1. has only one line, 2. x only occurs once so you can just plug in a complex expression 3. no temporary variables are left min(sapply(x, function(z) if (z > 0) z else Inf)) (function(z) min(ifelse(z > 0, z, Inf))) (x) with(list(z = x), min(z[z > 0])) local({ z <- x; min(z[z > 0]) }) On 9/10/07, dxc13 <dxc13 at health.state.ny.us> 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 > -- > View this message in context: http://www.nabble.com/finding-the-minimum-positive-value-of-some-data-tf4417250.html#a12599319 > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >
Either> min(diff(sort(x))[diff(sort(x))>0])or> min(diff(sort(unique(x))))--- dxc13 <dxc13 at health.state.ny.us> 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 > -- > View this message in context: >http://www.nabble.com/finding-the-minimum-positive-value-of-some-data-tf4417250.html#a12599319> Sent from the R help mailing list archive at > Nabble.com. > > ______________________________________________ > 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. >
Vladimir Eremeev
2007-Sep-11 08:23 UTC
[R] finding the minimum positive value of some data
min(x[x>0]) 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 >-- View this message in context: http://www.nabble.com/finding-the-minimum-positive-value-of-some-data-tf4417250.html#a12610228 Sent from the R help mailing list archive at Nabble.com.