[R] How to Calculate the Mean by Multiple Groups in R
Luigi Marongiu
m@rong|u@|u|g| @end|ng |rom gm@||@com
Tue Oct 24 16:36:40 CEST 2023
Thank you
On Tue, Oct 24, 2023 at 3:01 PM peter dalgaard <pdalgd using gmail.com> wrote:
>
> Also,
>
> > aggregate(cbind(OD, ODnorm) ~ Time + Target + Conc, data = df, FUN = "mean")
> Time Target Conc OD ODnorm
> 1 1 BACT 1 765.3333 108.33333
> 2 1 BACT 2 745.3333 88.33333
> 3 1 BACT 3 675.0000 18.00000
>
> (You might wish for "cbind(OD,ODnorm) ~ . - Well", but aggregate.formula is not smart enough for that.)
>
> -pd
>
> > On 24 Oct 2023, at 14:40 , Sarah Goslee <sarah.goslee using gmail.com> wrote:
> >
> > Hi,
> >
> > I think you're misunderstanding which set of variables go on either
> > side of the formula.
> >
> > Is this what you're looking for?
> >
> >> aggregate(OD ~ Time + Target + Conc, data = df, FUN = "mean")
> > Time Target Conc OD
> > 1 1 BACT 1 765.3333
> > 2 1 BACT 2 745.3333
> > 3 1 BACT 3 675.0000
> >> aggregate(ODnorm ~ Time + Target + Conc, data = df, FUN = "mean")
> > Time Target Conc ODnorm
> > 1 1 BACT 1 108.33333
> > 2 1 BACT 2 88.33333
> > 3 1 BACT 3 18.00000
> >
> > Or using a different form, that might be more straightforward to you:
> >
> >> aggregate(df[, c("OD", "ODnorm")], by = df[, c("Time", "Target", "Conc")], data = df, FUN = "mean")
> > Time Target Conc OD ODnorm
> > 1 1 BACT 1 765.3333 108.33333
> > 2 1 BACT 2 745.3333 88.33333
> > 3 1 BACT 3 675.0000 18.00000
> >
> > Sarah
> >
> > On Tue, Oct 24, 2023 at 8:31 AM Luigi Marongiu <marongiu.luigi using gmail.com> wrote:
> >>
> >> Hello,
> >> I have a data frame with different groups (Time, Target, Conc) and
> >> each entry has a triplicate value of the measurements OD and ODnorm.
> >> How can I merge the triplicates into a single mean value?
> >> I tried the following:
> >> ```
> >> df = data.frame(Time=rep(1, 9), Well=paste("A", 1:9, sep=""),
> >> OD=c(666, 815, 815, 702, 739, 795, 657, 705, 663),
> >> Target=rep("BACT", 9),
> >> Conc=c(1,1,1,2,2,2,3,3,3),
> >> ODnorm=c(9, 158, 158, 45, 82, 138, 0, 48, 6),
> >> stringsAsFactors = FALSE)
> >> aggregate(.~ODnorm, df, mean)
> >>
> >>> aggregate(.~ODnorm, df, mean)
> >> ODnorm Time Well OD Target Conc
> >> 1 0 NA NA NA NA NA
> >> 2 6 NA NA NA NA NA
> >> 3 9 NA NA NA NA NA
> >> 4 45 NA NA NA NA NA
> >> 5 48 NA NA NA NA NA
> >> 6 82 NA NA NA NA NA
> >> 7 138 NA NA NA NA NA
> >> 8 158 NA NA NA NA NA
> >>
> >> aggregate(cbind(Time, Target, Conc) ~ ODnorm, df, mean)
> >> ODnorm Time Target Conc
> >> 1 0 NA NA NA
> >> 2 6 NA NA NA
> >> 3 9 NA NA NA
> >> 4 45 NA NA NA
> >> 5 48 NA NA NA
> >> 6 82 NA NA NA
> >> 7 138 NA NA NA
> >> 8 158 NA NA NA
> >> ```
> >>
> >> Thank you.
> >>
> >> ______________________________________________
> >> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> >> 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.
> >
> >
> >
> > --
> > Sarah Goslee (she/her)
> > http://www.numberwright.com
> >
> > ______________________________________________
> > R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > 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.
>
> --
> Peter Dalgaard, Professor,
> Center for Statistics, Copenhagen Business School
> Solbjerg Plads 3, 2000 Frederiksberg, Denmark
> Phone: (+45)38153501
> Office: A 4.23
> Email: pd.mes using cbs.dk Priv: PDalgd using gmail.com
>
--
Best regards,
Luigi
More information about the R-help
mailing list