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]]
Frank <rggefrm at ucl.ac.uk> writes:> aggregate(frame[,c(-1)],list(frame$Day,frame$Time),mean) > > 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.No. But mean() will give an NA result if any vaues are NA.> I'm now hoping for some help so that the mean is still calculated in > this group.add na.rm=TRUE -- O__ ---- Peter Dalgaard ?ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
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. >
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