Skip to content
Prev 165276 / 398506 Next

NA, where no NA should (could!) be!

On Dec 20, 2008, at 6:26 PM, Oliver Bandel wrote:

            
It does not take an expert. All you need to do is read the help page.   
Dalgaard already diagnosed the problem. Look at his example and see  
what your "solution" does to it.

 > x <- factor(1,levels=1:2)
 >  tapply(1,x,sum)
  1  2
  1 NA

 > x <- factor(1,levels=1:2)
 >  tapply(1,factor(x),sum)
1
1

The function, factor, applied to a factor with unused levels discards  
those levels.

 From the factor help page:
"Normally the ?levels? used as an attribute of the result are the  
reduced set of levels after removing those in exclude, but this can be  
altered by supplying labels."

Since NA is the default for exclude, that results in the "trimming  
down" that you see with the application of factor(.)