R中不同变量的CAGR计算
CAGR Calculation for different variables in R
这两张照片启发了我。首先,我想为每个值都取一个值,所以我将它们组合在一起,并对每年取平均值。
试试这个...
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()
由 reprex package (v2.0.1)
于 2022-05-07 创建
这两张照片启发了我。首先,我想为每个值都取一个值,所以我将它们组合在一起,并对每年取平均值。
试试这个...
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()
由 reprex package (v2.0.1)
于 2022-05-07 创建