[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 14:31:11 CEST 2023


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.



More information about the R-help mailing list