[R] ave gives unexpected NA's
Peter Dalgaard
p.dalgaard at biostat.ku.dk
Fri Oct 22 17:04:10 CEST 2004
Wolfram Fischer <wolfram at fischer-zim.ch> writes:
> [R 2.0.0 on Linux]
>
> I tried:
> > df <- data.frame(
> grp1=factor( c('A' ,'A' ,'A' ,'D', 'D' ) ) ,
> grp2=factor( c('a1','a2','a2','d1','d1') )
> )
> > df
> 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:
> > with( df, ave( val, grp1, grp2, FUN=sum ) )
> [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?
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.
--
O__ ---- Peter Dalgaard Blegdamsvej 3
c/ /'_ --- Dept. of Biostatistics 2200 Cph. N
(*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
More information about the R-help
mailing list