2

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

Community
  • 1
  • 1
  • 1
    I am not familiar with "hey" tag. Who makes "hey" and what is it used for. Is there an open source implementation of "hey"? – James A Mohler Dec 03 '12 at 23:24
  • Also dups: http://stackoverflow.com/questions/9593056/i-would-like-to-group-the-rows-of-this-dataset-by-index-and-then-sum-the-rows-by/9593529 – thelatemail Dec 03 '12 at 23:44

2 Answers2

3

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.

Paul Hiemstra
  • 59,984
  • 12
  • 142
  • 149
0

Or a base R version:

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
thelatemail
  • 91,185
  • 12
  • 128
  • 188