how apply a function across runs
Possible Duplicate:
R Grouping functions: sapply vs. lapply vs. apply. vs. tapply vs. by vs. aggregate vs
I have a model output file which looks like:
run step x
1 1 1
1 2 4
1 3 3
1 1 4
1 2 5
1 3 6
2 1 5
2 2 4
2 3 7
2 1 3
. . . and I need to calculate the mean values for each step according to the run number.How can I do this? Many thanks to anyone, who can help me. Viola
If I understand you correctly, this can be done using ddply from the plyr package:
require(plyr)
ddply(model_output, .(run, step), summarise, mn = mean(x))
Where model_output
is the model output you read from file.
或者一个基本的R版本:
aggregate(test["x"],test[c("run","step")],mean)
run step x
1 1 1 2.5
2 2 1 4.0
3 1 2 4.5
4 2 2 4.0
5 1 3 4.5
6 2 3 7.0
链接地址: http://www.djcxy.com/p/38286.html
上一篇: 分组因子,数据框和tapply存在问题
下一篇: 跨运行如何应用功能