2012-12-03 49 views
2

可能重复:
R Grouping functions: sapply vs. lapply vs. apply. vs. tapply vs. by vs. aggregate vs如何应用功能跨越运行

我看起来像一个模型输出文件:

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 

。 。 。 我需要根据跑步数来计算每一步的平均值。我该如何做?非常感谢任何人,谁可以帮助我。 中提琴

+1

我不熟悉的 “嘿” 的标签。谁制造“嘿”,它用于什么。有没有开源的“嘿”实现? –

+0

也dups:http://stackoverflow.com/questions/9593056/i-would-like-to-group-the-rows-of-this-dataset-by-index-and-then-sum-the-rows-通过/ 9593529 – thelatemail

回答

3

如果我理解正确的话,这可以通过使用ddply从plyr包来完成:

require(plyr) 
ddply(model_output, .(run, step), summarise, mn = mean(x)) 

哪里model_output是你从文件中读取模型输出。

+1

耶稣,这是快!谢谢! – user1873902

0

还是一个基础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