I am trying to calculate diameter growth for a set of trees over a number of years in a dataframe in which each row is a given tree during a given year. Typically, this sort of data has each individual stem as a single row with that stem's diameter for each year given in a separate column, but for various reasons, this dataframe needs to remain such that each row is an individual stem in an individual year. A simplistic model version of the data would be as follows

```
df<-data.frame("Stem"=c(1:5,1:5,1,2,3,5,1,2,3,5,6),
           "Year"=c(rep(1997,5), rep(1998,5), rep(1999,4), rep(2000,5)),
           "Diameter"=c(1:5,seq(1.5,5.5,1),2,3,4,6,3,5,7,9,15))
df
   Stem Year  DAP
1     1 1997  1.0
2     2 1997  2.0
3     3 1997  3.0
4     4 1997  4.0
5     5 1997  5.0
6     1 1998  1.5
7     2 1998  2.5
8     3 1998  3.5
9     4 1998  4.5
10    5 1998  5.5
11    1 1999  2.0
12    2 1999  3.0
13    3 1999  4.0
14    5 1999  6.0
15    1 2000  3.0
16    2 2000  5.0
17    3 2000  7.0
18    5 2000  9.0
19    6 2000 15.0

```

What I am trying to accomplish is to make a new column that takes the diameter for a given stem in a given year and subtracts the diameter for that same stem in the previous year. I assume that this will require some set of nested for loops. Something like 

```
for (i in 1:length(unique(df$Stem_ID){
  for (t in 2:length(unique(df$Year){
.....
  }
}

```

What I'm struggling with is how to write the function that calculates:
Diameter[t]-Diameter[t-1] for each stem. Any suggestions would be greatly appreciated.