R: Bugs or 'features'

From MathWiki

Possible bugs:

  • model.frame
Using 'na.action= na.include' creates broken factors by failing to create an '<NA>' level
e.g.
 > dd <- data.frame ( a = c('a','a','b',NA,'b'), b = rep(c('A','B'), c(3,2)))
 > dd
      a b
 1    a A
 2    a A
 3    b A
 4 <NA> B
 5    b B
 > unclass(dd$a)        # note that the factor index is NA -- this is what a factor should look like
                        # when NAs are not 'included'
 [1]  1  1  2 NA  2
 attr(,"levels")
 [1] "a" "b"
 > mf = model.frame ( ~ a + b, dd, na.action = na.include)
 > mf                   # everything looks okay
      a b
 1    a A
 2    a A
 3    b A
 4 <NA> B
 5    b B
 > unclass( mf$a)     # however the factor index is 3 for NAs but there is no corresponding level
 [1] 1 1 2 3 2
 attr(,"levels")
 [1] "a" "b"
 > unclass( na.include( dd )$a)    # this shows what a factor with included NAs should look like
 [1] 1 1 2 3 2
 attr(,"levels")
 [1] "a" "b" NA