2012-12-03 53 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