[R] is.constant 2

Duncan Murdoch murdoch at stats.uwo.ca
Wed Sep 22 14:40:40 CEST 2004


On Wed, 22 Sep 2004 12:16:10 +0000 (UTC), Gabor Grothendieck
<ggrothendieck at myway.com> wrote :

>Suggest you use an S3 generic and a separate methods for factor, and
>in the future, other classes.  

That's not a bad idea, but is it really worth the trouble?  Why not
piggyback on the unique() generic, and define it as something like

is.constant <- function(x, na.rm = FALSE, ...) {
  vals <- unique(x, ...)
  if (na.rm) vals <- vals[!is.na(vals)]
  
  # What should the value be for c(1, NA)?  If FALSE is wanted,

  length(vals) == 1

  # but if NA is desired

  #  ifelse (any(is.na(vals)),  NA, length(vals) == 1)
}

>Also to make it more consistent with
>other R functions have an na.rm= argument which defaults to TRUE.

The more common default is FALSE.

Duncan Murdoch




More information about the R-help mailing list