[R] is.constant

Liaw, Andy andy_liaw at merck.com
Wed Sep 22 12:31:31 CEST 2004


> Christian Hoffmann
> 
> >>x <- c(1, 2, NA)
> >>is.constant(x)
> > 
> > [1] TRUE
> > 
> > For data such as c(1, 1, 1, NA), I should think the safest 
> answer should be
> > NA, because one really doesn't know whether that last 
> number is 1 or not.
> > 
> > Andy
> >  
> My version is
> is.constant <-  function(x) {
>    if (is.numeric(x) & !any(is.na(x)))  identical(min(x), 
> max(x)) else FALSE
> }
> 
> rendering
>  > is.constant(c(1,1,NA))
> [1] FALSE

As I said, the `safest' thing is to return NA, as the NA could be 1, or it
could be something else.  We just don't know.  It's probably a cleaner style
to have is.constant() return NA in the case that the input contains contants
and some NAs, and TRUE or FALSE otherwise; e.g., is.constant(c(1,2,NA))
should clearly be FALSE.  Then the output of is.constant() should be checked
for possible NA.

Just my $0.02...

Andy

>  > is.constant(c(1,1,NaN))
> [1] FALSE
>  > is.constant(rep(c(sin(pi/2),1),10)) # TRUE
> [1] TRUE
> -- 
> Dr.sc.math.Christian W. Hoffmann, 
> http://www.wsl.ch/staff/christian.hoffmann
> Mathematics + Statistical Computing   e-mail: 
> christian.hoffmann at wsl.ch
> Swiss Federal Research Institute WSL  Tel: ++41-44-73922-   
> -77  (office)
> CH-8903 Birmensdorf, Switzerland             -11(exchange), -15  (fax)
> 
> ______________________________________________
> 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
> 
>




More information about the R-help mailing list