计算特定列上的行平均值

时间:2014-03-25 11:04:00

标签: r statistics

每个样本我有六个重复。我想计算列读数的每个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

3 个答案:

答案 0 :(得分: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)

答案 1 :(得分:1)

您可以使用aggregate

aggregate(Reading~Sample,data=yourdata, mean)

答案 2 :(得分:0)

arr <- tapply(dframe$Reading, dframe$Sample, mean)

将给出一个数组,其中包含每个样本的阅读方式。如果要创建结果的data.frame

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