Skip to content

ave gives unexpected NA's

2 messages · wolfram at fischer-zim.ch, Peter Dalgaard

#
[R 2.0.0 on Linux]

I tried:
grp1=factor( c('A' ,'A' ,'A' ,'D', 'D' ) ) ,
     grp2=factor( c('a1','a2','a2','d1','d1') )
     )
grp1 grp2 val
1    A   a1   1
2    A   a2   2
3    A   a2   4
4    D   d1   8
5    D   d1  16

I got:
[1]  1 24 24 NA NA

I have expected to get:
[1]  1 6 24

Do I misunderstand something with `ave' or is there a bug?

Thanks - Wolfram
#
Wolfram Fischer <wolfram at fischer-zim.ch> writes:
Both. You should have expected 1 6 6 24 24.

Apparently the culprit is this line inside ave()

  unlist(lapply(split(x, g), FUN))[g]

which acts up if there are empty levels of g. It works with

  unlist(lapply(split(x, g), FUN))[as.character(g)]

but that doesn't strike me as the right solution.