[R] How to calculate means for multiple variables in samples with different sizes

Berend Hasselman bhh at xs4all.nl
Fri Mar 11 12:11:37 CET 2011


Aline Santos wrote:
> 
> Hello R-helpers:
> 
> I have data like this:
> 
> sample    replicate    height    weight    age
> A    1.00    12.0    0.64    6.00
> A    2.00    12.2    0.38    6.00
> A    3.00    12.4    0.49    6.00
> B    1.00    12.7    0.65    4.00
> B    2.00    12.8    0.78    5.00
> C    1.00    11.9    0.45    6.00
> C    2.00    11.84    0.44    2.00
> C    3.00    11.43    0.32    3.00
> C    4.00    10.24    0.84    4.00
> D    1.00    14.2    0.54    2.00
> D    2.00    15.67    0.67    7.00
> D    3.00    15.11    0.81    7.00
> 
> Now, how can I calculate the mean for each condition (heigth, weigth, age)
> in each sample, considering the samples have different number of
> replicates?
> 
> 
> The final matrix should look like:
> 
> sample    height    weight    age
> A    12.20    0.50    6.00
> B     12.75      0.72      4.50
> C     11.35      0.51      3.75
> D     14.99      0.67      5.33
> 
> This is a simplified version of my dataset, which consist of 100 samples
> (unequally distributed in 530 replicates) for 600 different conditions.
> 

con.data <- textConnection("sample    replicate    height    weight    age 
A    1.00    12.0    0.64    6.00 
A    2.00    12.2    0.38    6.00 
A    3.00    12.4    0.49    6.00 
B    1.00    12.7    0.65    4.00 
B    2.00    12.8    0.78    5.00 
C    1.00    11.9    0.45    6.00 
C    2.00    11.84    0.44    2.00 
C    3.00    11.43    0.32    3.00 
C    4.00    10.24    0.84    4.00 
D    1.00    14.2    0.54    2.00 
D    2.00    15.67    0.67    7.00 
D    3.00    15.11    0.81    7.00 ")

df <- read.table(con.data,header=TRUE)
close(con.data)
aggregate(df[,!names(df) %in% c("sample","replicate")
],by=list(sample=df$sample), FUN=mean)

best regards

Berend

--
View this message in context: http://r.789695.n4.nabble.com/How-to-calculate-means-for-multiple-variables-in-samples-with-different-sizes-tp3347819p3347895.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list