[R] frustration with ave()

Thomas W Blackwell tblackw at umich.edu
Sun Sep 28 22:05:43 CEST 2003


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]) .

However, I have not thought through whether  ave()  could ever
be called with a grouping variable of dimension greater than 1,
or what consequence that would have for the proposed fix.

-  tom blackwell  -  u michigan medical school  -  ann arbor  -

On Sat, 27 Sep 2003, Ed Hagen wrote:

> I'm confused why I'm getting NA's in the output from ave() (at the end). Any
> help would be greatly appreciated.  I'm including the data in case that is where
> the problem lies:
>
>  > f <- factor(FAMILYID)
>  > bodyfat <- na.omit(data.frame(loessBODYFAT, f))
>  > bodyfat$loessBODYFAT
>      ...
>  > bodyfat$f
>    [1] 1  1  1  1  1  2  3  3  3  3  3  3  3  3  3  3  4  5  6  6  6  7  7
>   [24] 7  7  8  9  9  9  9  9  9  9  9  9  9  9  10 10 10 11 11 11 11 12 12
>   [47] 12 12 13 13 13 14 14 14 15 15 15 15 15 16 16 16 16 16 16 16 16 18 18
>   [70] 18 18 20 21 21 22 22 22 23 23 23 23 23 25 25 25 30 30 30 31 31 31 31
>   [93] 35 35 35 35 35 35 37 37 39 39 39 39 39 39 39 39 40 40 44 44 45 45 45
> [116] 47 47 47 47 50 50 50
> 50 Levels: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 ... 50
>  > ave(bodyfat$loessBODYFAT, bodyfat$f)
>      ...
>   [81] -6.250627e-01 -6.250627e-01 -6.066309e-01 -6.066309e-01 -6.066309e-01
>   [86]  2.602143e-01  2.602143e-01  2.602143e-01 -2.354473e-01 -2.354473e-01
>   [91] -2.354473e-01 -2.354473e-01            NA            NA            NA
>   [96]            NA            NA            NA            NA            NA
> [101]            NA            NA            NA            NA            NA
> [106]            NA            NA            NA            NA            NA
> [111]            NA            NA            NA            NA            NA
> [116]            NA            NA            NA            NA            NA
> [121]            NA            NA
>
> Thanks in advance,
>
> Edward H. Hagen                  Institute for Theoretical Biology
> phone: +49/30 2093-8649             Humboldt-Universität zu Berlin
> fax:   +49/30 2093-8801                         Invalidenstraße 43
> http://itb.biologie.hu-berlin.de/~hagen      10115 Berlin, Germany




More information about the R-help mailing list