[R] Calculate aggregate differences

crmontes at ncsu.edu crmontes at ncsu.edu
Thu Nov 1 17:09:34 CET 2007


Hi everyone

I am trying to summarize a table with yield estimates of a forest
plantation.  For that I have four blocks and four treatments measured over
a period of 10 years (every year). In each plot trees are measured
(diameters and heights).

With aggregate function I can calculate the average diameter or the total
volume for each plot at any time with something like this:

my_agg <- aggregate(list(Ht = HTO, Dbh = DBH), list(Age = AGE, Block =
BLOCK, Treat = TREAT), sum)

my_agg <- aggregate(list(VOL = VOLUME), list(Age = AGE, Block = BLOCK,
Treat = TREAT), sum)

where HTO is the height of the tree
      Ht  average height of trees in each plot at time t
      DBH is the diameter at 1.3 meter height
      Dbh is the average diameter of the plot at time t
      VOL is the [i]th tree volume
      Vol is the volume of the plot at time t

To do actual growth analysis I need to calculate the difference in volume
between t and (t-1) for every block/treatment.

Now the question

How can I use aggregate to calculate the difference of each time series
and have an output like aggregate does?  I know it is imposible to use
diff() within aggregate because it gives me an scalar.
In SAS proc means handles such a thing, and diff() in R is fine for a
single series, but in this case I want to calculate for all the
treatments/blocks at the same time.

I can do the whole using by and some more less elegant procedures, but I
figure there should be a cleaner way as in PROC MEANS.

Any suggestions?

Cristian Montes
NC State University



More information about the R-help mailing list