Reputation: 23
I'm analyzing the management in rotation forestry where the forest is planted, then as it grows it is thinned a few times by harvesting some trees and then, in the end, all trees are cut down.
Here's an example of what my data could look like:
year <- rep(seq(1,10, 1),3)
stand <- c(rep(1,10),rep(2,10),rep(3,10))
treatment <- c("planting", "none", "thinning", "none", "thinning", "none", "none", "felling", "planting", "none",
"none", "planting", "thinning", "thinning", "thinning","none", "felling", "planting", "thinning", "none",
"planting", "none", "thinning","none", "felling", "planting", "thinning","none", "thinning", "felling")
data <- data.frame(year,stand,treatment)
The three stands have very different rotations.
I have thousands of stands with all sorts of rotation schedules, so it is impossible to count this manually for me. How can I write a script to calculate the number of thinnings per complete rotation (the years in between planting and felling)?
In the end, I would like to have a table with the stand number and number of thinnings per complete rotation. All incomplete rotations should be discarded.
Upvotes: 2
Views: 45
Reputation: 388982
For every stand
we can keep everything between first 'planting'
and last 'felling'
since we want to consider only complete rotation. We then calculate number of 'thinning'
and divide it by number of 'planting'
which happened in that stand
to get thinning per rotation.
library(dplyr)
data %>%
group_by(stand) %>%
filter(between(row_number(), min(which(treatment == 'planting')),
max(which(treatment == 'felling')))) %>%
summarise(thin_per_rotation = sum(treatment == 'thinning')/
sum(treatment == 'planting'))
# stand thin_per_rotation
# <dbl> <dbl>
#1 1 2
#2 2 3
#3 3 1.5
Upvotes: 1