# How to detect if a vector is FP constant?

 Classic List Threaded
5 messages
Reply | Threaded
Open this post in threaded view
|

## How to detect if a vector is FP constant?

 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/______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

## Re: How to detect if a vector is FP constant?

 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/> > ______________________________________________ > [hidden email] 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 [hidden email] ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

## Re: How to detect if a vector is FP constant?

 In reply to this post by Hadley Wickham-2 On Mon, Nov 8, 2010 at 4:45 PM, Hadley Wickham <[hidden email]> 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 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. 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/> > ______________________________________________ > [hidden email] 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/______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

## Re: How to detect if a vector is FP constant?

 > 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/______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

## Re: How to detect if a vector is FP constant?

 The games we play in zapsmall might apply to your current question. zapsmall rounds to a specified number of digits. The initial intent of zapsmall was to display 10^(-16) as 0. Rich > tmp <- c(1, 1 + c(-1,1)*10^(-14)) > tmp [1] 1 1 1 > unique(tmp) [1] 1 1 1 > unique(zapsmall(tmp)) [1] 1 >         [[alternative HTML version deleted]] ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.