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 at 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 at 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
Luigi Marongiu
2023-Oct-24 12:43 UTC
[R] How to Calculate the Mean by Multiple Groups in R
Thank you, the last is exactly what I was looking for. On Tue, Oct 24, 2023 at 2:41?PM Sarah Goslee <sarah.goslee at 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 at 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 at 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-- Best regards, Luigi
peter dalgaard
2023-Oct-24 13:01 UTC
[R] How to Calculate the Mean by Multiple Groups in R
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 at 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 at 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 at 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 at 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 at cbs.dk Priv: PDalgd at gmail.com
Apparently Analagous Threads
- How to Calculate the Mean by Multiple Groups in R
- How to Calculate the Mean by Multiple Groups in R
- How to invert axis within defined range with ggplot2 scale_y_reverse?
- How to invert axis within defined range with ggplot2 scale_y_reverse?
- finite difference scheme for 2D differential equations