Skip to content
Prev 165271 / 398506 Next

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

Oliver Bandel wrote:
Why do so many people have such trouble with the word "reproducible"? We 
can't reproduce that without access to weblog_by_date!

Anyways I think it is tapply that is behaving unexpectedly to you:

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

which is kind of surprising since the sum over an empty set is usually 
zero. However, that _is_ what the documentation for tapply says:

      When 'FUN' is present, 'tapply' calls 'FUN' for each cell that has
      any data in it.  If 'FUN' returns a single atomic value for each
      such cell (e.g., functions 'mean' or 'var') and when 'simplify' is
      'TRUE', 'tapply' returns a multi-way array containing the values,
      and 'NA' for the empty cells.

a passable workaround is

 > sapply(split(1,x),sum)
1 2
1 0