2014-03-25 53 views
1

對於每個樣本,我有六個重複項。我想計算列讀數的每個sample_id的平均值。我想保持sample_id平均閱讀。數據看起來像這樣;計算特定列上的行平均數

Sample Replicate Number Reading 
S_1  1 1 7 
S_1  2 2 7 
S_1  3 1 7 
S_1  4 2 9 
S_1  5 1 9 
S_1  6 2 7 
S_2  1 1 6 
S_2  2 2 6 
S_2  3 1 9 
S_2  4 2 9 
S_2  5 1 9 
S_2  6 2 9 
S_3  1 1 1 
S_4  2 2 1 
S_5  3 1 1 
S_6  4 2 1 
S_7  5 1 2 
S_8  6 2 1 

所以我應該有

Sample Reading(mean) 
S_1 
S-2 
.. 
S_8 

回答

1

您可以使用aggregate

aggregate(Reading~Sample,data=yourdata, mean) 
2
test <- data.frame(Sample = c(rep("S_1", 6), rep("S_2", 6), "S_3", "S_4", "S_5", "S_6", "S_7", "S_8"), 
        Replicate = rep(1:6, 3)) 
aggregate(Replicate ~ Sample, test, mean) 
0
arr <- tapply(dframe$Reading, dframe$Sample, mean) 

會給每個樣品讀數爲數組。如果你想創建一個數據幀的結果

tableFrame <- data.frame(Sample = names(arr), "Reading(mean)" = arr, check.names=FALSE)