Hi all, What's the equivalent to length(unique(x)) == 1 if want to ignore small floating point differences? Should I look at diff(range(x)) or sd(x) or something else? What cut off should I use? If it helps to be explicit, I'm interested in detecting when a vector is constant for the purpose of visual display. In other words, if I rescale x to [0, 1] do I have enough precision to get at least 100 unique values. Thanks! Hadley -- Assistant Professor / Dobelman Family Junior Chair Department of Statistics / Rice University http://had.co.nz/
how about all.equal(x,rep(mean(x),length(x))) or all.equal(x,rep(mean(x),length(x), tolerance=...) albyn On Mon, Nov 08, 2010 at 06:45:00PM -0600, Hadley Wickham wrote:> Hi all, > > What's the equivalent to length(unique(x)) == 1 if want to ignore > small floating point differences? Should I look at diff(range(x)) or > sd(x) or something else? What cut off should I use? > > If it helps to be explicit, I'm interested in detecting when a vector > is constant for the purpose of visual display. In other words, if I > rescale x to [0, 1] do I have enough precision to get at least 100 > unique values. > > Thanks! > > Hadley > > -- > Assistant Professor / Dobelman Family Junior Chair > Department of Statistics / Rice University > http://had.co.nz/ > > ______________________________________________ > R-help at r-project.org 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. >-- Albyn Jones Reed College jones at reed.edu
On Mon, Nov 8, 2010 at 4:45 PM, Hadley Wickham <hadley at rice.edu> wrote:> Hi all, > > What's the equivalent to length(unique(x)) == 1 if want to ignore > small floating point differences? ?Should I look at diff(range(x)) orI think this does what you want (borrowing from all.equal.numeric): all(abs((x - mean(x))) < .Machine$double.eps^0.5) with a vector of length 1 million, it took .076 seconds on a fairly old system. Josh> sd(x) or something else? ?What cut off should I use? > > If it helps to be explicit, I'm interested in detecting when a vector > is constant for the purpose of visual display. ?In other words, if I > rescale x to [0, 1] do I have enough precision to get at least 100 > unique values. > > Thanks! > > Hadley > > -- > Assistant Professor / Dobelman Family Junior Chair > Department of Statistics / Rice University > http://had.co.nz/ > > ______________________________________________ > R-help at r-project.org 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. >-- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/
> I think this does what you want (borrowing from all.equal.numeric): > > all(abs((x - mean(x))) < .Machine$double.eps^0.5) > > with a vector of length 1 million, it took .076 seconds on a fairly old system.Hmmm, maybe I want: all.equal(min(x), max(x)) ? Hadley -- Assistant Professor / Dobelman Family Junior Chair Department of Statistics / Rice University http://had.co.nz/