Re: [R] How to Calculate the Mean by Multiple Groups in R
A variation is to remove Well and then we can use dot to refer to the remaining columns. aggregate(cbind(OD, ODnorm) ~ . , subset(df, select = - Well), mean) On Tue, Oct 24, 2023 at 8:32 AM Luigi Marongiu 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 > 7138 NA NA NA NA NA > 8158 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 > 7138 NA NA NA > 8158 NA NA NA > ``` > > Thank you. > > __ > R-help@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. -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com __ R-help@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.
Re: [R] How to Calculate the Mean by Multiple Groups in R
Thank you On Tue, Oct 24, 2023 at 3:01 PM peter dalgaard wrote: > > Also, > > > aggregate(cbind(OD, ODnorm) ~ Time + Target + Conc, data = df, FUN = "mean") > Time Target Conc ODODnorm > 11 BACT1 765. 108.3 > 21 BACT2 745. 88.3 > 31 BACT3 675. 18.0 > > (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 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 > > 11 BACT1 765. > > 21 BACT2 745. > > 31 BACT3 675. > >> aggregate(ODnorm ~ Time + Target + Conc, data = df, FUN = "mean") > > Time Target ConcODnorm > > 11 BACT1 108.3 > > 21 BACT2 88.3 > > 31 BACT3 18.0 > > > > 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 ODODnorm > > 11 BACT1 765. 108.3 > > 21 BACT2 745. 88.3 > > 31 BACT3 675. 18.0 > > > > Sarah > > > > On Tue, Oct 24, 2023 at 8:31 AM Luigi Marongiu > > 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 > >> 7138 NA NA NA NA NA > >> 8158 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 > >> 7138 NA NA NA > >> 8158 NA NA NA > >> ``` > >> > >> Thank you. > >> > >> __ > >> R-help@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@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@cbs.dk Priv: pda...@gmail.com > -- Best regards, Luigi __ R-help@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.
Re: [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 ODODnorm 11 BACT1 765. 108.3 21 BACT2 745. 88.3 31 BACT3 675. 18.0 (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 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 > 11 BACT1 765. > 21 BACT2 745. > 31 BACT3 675. >> aggregate(ODnorm ~ Time + Target + Conc, data = df, FUN = "mean") > Time Target ConcODnorm > 11 BACT1 108.3 > 21 BACT2 88.3 > 31 BACT3 18.0 > > 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 ODODnorm > 11 BACT1 765. 108.3 > 21 BACT2 745. 88.3 > 31 BACT3 675. 18.0 > > Sarah > > On Tue, Oct 24, 2023 at 8:31 AM Luigi Marongiu > 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 >> 7138 NA NA NA NA NA >> 8158 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 >> 7138 NA NA NA >> 8158 NA NA NA >> ``` >> >> Thank you. >> >> __ >> R-help@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@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@cbs.dk Priv: pda...@gmail.com __ R-help@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.
Re: [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 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 > 11 BACT1 765. > 21 BACT2 745. > 31 BACT3 675. > > aggregate(ODnorm ~ Time + Target + Conc, data = df, FUN = "mean") > Time Target ConcODnorm > 11 BACT1 108.3 > 21 BACT2 88.3 > 31 BACT3 18.0 > > 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 ODODnorm > 11 BACT1 765. 108.3 > 21 BACT2 745. 88.3 > 31 BACT3 675. 18.0 > > Sarah > > On Tue, Oct 24, 2023 at 8:31 AM Luigi Marongiu > 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 > > 7138 NA NA NA NA NA > > 8158 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 > > 7138 NA NA NA > > 8158 NA NA NA > > ``` > > > > Thank you. > > > > __ > > R-help@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 __ R-help@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.
Re: [R] How to Calculate the Mean by Multiple Groups in R
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 11 BACT1 765. 21 BACT2 745. 31 BACT3 675. > aggregate(ODnorm ~ Time + Target + Conc, data = df, FUN = "mean") Time Target ConcODnorm 11 BACT1 108.3 21 BACT2 88.3 31 BACT3 18.0 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 ODODnorm 11 BACT1 765. 108.3 21 BACT2 745. 88.3 31 BACT3 675. 18.0 Sarah On Tue, Oct 24, 2023 at 8:31 AM Luigi Marongiu 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 > 7138 NA NA NA NA NA > 8158 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 > 7138 NA NA NA > 8158 NA NA NA > ``` > > Thank you. > > __ > R-help@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@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.
[R] How to Calculate the Mean by Multiple Groups in R
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 7138 NA NA NA NA NA 8158 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 7138 NA NA NA 8158 NA NA NA ``` Thank you. __ R-help@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.