[R] aggregate function with 'NA'

From: Frank <rggefrm_at_ucl.ac.uk>
Date: Sun 01 Oct 2006 - 11:15:58 GMT


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@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. Received on Sun Oct 01 22:16:19 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Sun 01 Oct 2006 - 13:30:07 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.