撰寫了一個計算面積、產量和降雨量的年增長的 RStudio 函式。
}
我的資料集包括 3 種不同的作物型別。
這兩張照片給了我很大的啟發。首先,我想在我的資料集中為每種作物和每年獲取一個值。因此,我將它們組合在一起并平均每年。現在如何根據最終值(最大年份)和起始值(最小年份)計算每種作物的復合年增長率?所以我想我最終只會得到每個值。那會是真的嗎?
uj5u.com熱心網友回復:
試試這個 ...
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 包創建于 2022-05-07 (v2.0.1)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/470299.html
下一篇:在python中列印出素數串列