[R] aggregate function with 'NA'
Johan Sandblom
jsandblom at gmail.com
Sun Oct 1 14:23:38 CEST 2006
aggregate(frame[,c(-1)],list(frame$Day,frame$Time),mean, na.rm=T)
2006/10/1, Frank <rggefrm at ucl.ac.uk>:
> 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.
>
--
Johan Sandblom N8, MRC, Karolinska sjh
t +46851776108 17176 Stockholm
m +46735521477 Sweden
"What is wanted is not the will to believe, but the
will to find out, which is the exact opposite"
- Bertrand Russell
More information about the R-help
mailing list