Wrote a RStudio function calculating annual growth of Area, Yield and Rainfall.
}
My dataset include 3 different crop types.
I was inspired by these 2 photos. First I wanted to get a single value for each crop and each year in my dataset. So I grouped them together and averaged them for each year. Now how can I calculate CAGR for each crop based on final value (max year) and starting value (min year)? So I thought I'd end up with only 1 value for each. Would that be true?
CodePudding user response:
Try this ...
library(tidyverse)
library(scales)
#>
#> Attaching package: 'scales'
#> The following object is masked from 'package:purrr':
#>
#> discard
#> The following object is masked from 'package:readr':
#>
#> col_factor
growth <- function(x, y){
((x / first(x)) ^ (1 / (y - first(y)))) - 1
}
data_df <- tribble(
~Crop, ~Year, ~Area, ~Yield, ~Rainfall,
"Coconut ", 2000L, 18168, 3583.22324966975, 2763.2,
"Coconut ", 2001L, 18190, 3542.05607476636, 3080.9,
"Coconut ", 2002L, 18240, 3700.10964912281, 2620.2,
"Coconut ", 2003L, 18284.74, 3750.66859031083, 2355.9,
"Coconut ", 2004L, 18394.7, 2847.5593513349, 2460.1,
"Coconut ", 2005L, 13876.57, 3749.48564378661, 2954.7,
"Coconut ", 2006L, 14358, 4134.97701629753, 2404.7,
"Rice", 2000L, 102, 3.14705882352941, 2763.2,
"Rice", 2001L, 83, 3.6144578313253, 3080.9,
"Rice", 2002L, 189.2, 2.7, 2620.2,
"Rice", 2003L, 52, 1.73403846153846, 2355.9,
"Rice", 2004L, 52.94, 1.37079712882508, 2460.1,
"Rice", 2005L, 2.09, 5.77033492822967, 2954.7,
"Rice", 2006L, 6854.3, 2.77134353617437, 2404.7,
"Sugarcane", 2000L, 1, 2, 2763.2,
"Sugarcane", 2001L, 1, 1, 3080.9,
"Sugarcane", 2002L, 5, 8, 2620.2,
"Sugarcane", 2003L, 268, 10.7444776119403, 2355.9,
"Sugarcane", 2006L, 0.2, 2.5, 2404.7
)
data_df |>
group_by(Crop) |>
mutate(across(c(Area, Rainfall, Yield), ~ growth(., Year), .names = "{.col}_cagr")) |>
slice_tail(n = 1) |>
pivot_longer(ends_with("_cagr")) |>
ggplot(aes(Crop, value, fill = name))
geom_col(position = "dodge")
scale_y_continuous(labels = label_percent())
labs(x = NULL, y = "CAGR", fill = NULL)
theme_bw()
Created on 2022-05-07 by the reprex package (v2.0.1)