Skip to content
Prev 275684 / 398506 Next

Consistant test for NAs in a factor when exclude = NULL?

Note that for factors with NA in the levels, is.na(f)[2] <- TRUE
and is.na(f[2])<-TRUE give different results:

  > f <- factor(c("A","A",NA), levels=c(NA, "A"), exclude=NULL)
  > str(f)
   Factor w/ 2 levels NA,"A": 2 2 1
  > is.na(f)
  [1] FALSE FALSE FALSE

  > is.na(f[2]) <- TRUE
  > str(f)
   Factor w/ 2 levels NA,"A": 2 1 1
  > is.na(f)
  [1] FALSE FALSE FALSE

  > is.na(f)[2] <- TRUE
  > str(f)
   Factor w/ 2 levels NA,"A": 2 NA 1
  > is.na(f)
  [1] FALSE  TRUE FALSE

  > f[2] <- NA
  > str(f)
   Factor w/ 2 levels NA,"A": 2 1 1
  > is.na(f)
  [1] FALSE FALSE FALSE

You may find it easiest to change the NA's to strings
with a different name.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com