[R] frustration with ave()

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Sun Sep 28 22:28:03 CEST 2003


Thomas W Blackwell <tblackw at umich.edu> writes:

> Ed  -
> 
> You seem to have encountered a bug.  I can reproduce Ed's difficulty
> in a completely artificial example in which there are unused levels :
> 
> tmp <- factor(rep(seq(10), seq(10)))     #  length(tmp) # [1] 55
> ave(seq(50), tmp[-seq(5)])               #  gives NA in rows 32-50
> 
> I would consider this to be incorrect behavior for the function
> ave().  For the base package maintainers, I would suggest modifying
> the definition of  ave()  so that the line involving  as.factor()
> reads :
> 
>          l[[i]] <- li <- as.factor(l[[i]][,drop=T]) .

A minimal version of the same effect would be

> ave(1:2,factor(2:3,levels=1:3))
[1]  2 NA

Your fix looks sensible to me, but it might be better with simply

>          l[[i]] <- li <- factor(l[[i]]) 


-- 
   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