[R] aggregate function with 'NA'

Gabor Grothendieck ggrothendieck at gmail.com
Sun Oct 1 14:20:10 CEST 2006


See ?mean and note the na.rm= argument:

aggregate(frame[-1], frame[1:2], mean, na.rm = TRUE)


On 10/1/06, Frank <rggefrm at ucl.ac.uk> wrote:
> Dear r-help reader,
>
> I have some problems with the aggregate function.
>
> My datframe looks like
>  >frame
>
>   Day Time V1 V2
> 1   M    0  3 NA
> 2   M    0  4 NA
> 3   M    0  5  2
> 4   M    1 NA  4
> 5   M    1 10  6
> 6   T    0  4 45
> 7   T    1  4  3
> 8   T    1  3  2
> 9   T    1  6  1
>
> I used the aggegate function to obtain the mean in V1 and V2 over the
> grouping variable
> Time and Day
>
>  aggregate(frame[,c(-1)],list(frame$Day,frame$Time),mean)
>   Group.1 Group.2 Time       V1 V2
> 1       M       0    0 4.000000 NA
> 2       T       0    0 4.000000 45
> 3       M       1    1       NA  5
> 4       T       1    1 4.333333  2
>  >
>
> My problem is now that I do not obtain a 'mean' for Day=M/Time=0 and
> Day=M/Time=1,
>
> because aggregate ignores all values for a grouping variable if NA
> occurs.
>
> I'm now hoping for some help so that the mean is still calculated in
> this group.
>
> My table should look like:
>
>  aggregate(frame[,c(-1)],list(frame$Day,frame$Time),mean)
>   Group.1 Group.2 Time       V1 V2
> 1       M       0    0 4.000000 2
> 2       T       0    0 4.000000 45
> 3       M       1    1       10  5
> 4       T       1    1 4.333333  2
>
>  >
>
> I hope my description makes sense and appreciate any help.
>
> Yours
> Frank
>
>        [[alternative text/enriched version deleted]]
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list