[R] How to detect if a vector is FP constant?

Joshua Wiley jwiley.psych at gmail.com
Tue Nov 9 02:40:38 CET 2010


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)) 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/
>
> ______________________________________________
> 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/



More information about the R-help mailing list