我的数据集如下:让我们将其称为data_set_final
markets city base year_2018 year_2019 year_2020
1 AZ Bell 170104.516 0.448619 0.4019198 0.3262959
2 AZ Benson 1343.781 0.448619 0.4019198 0.3262959
3 AZ Buckeye 34155.261 0.448619 0.4019198 0.3262959
我想计算每月的CAGR值,我正在使用dplyr软件包进行计算
cagr_monthly <- data_set_final %>%
group_by (markets,city) %>%
mutate (Jan_2018 = base * (((1 + year_2018)^(1/12))-1) + base) %>%
mutate (Feb_2018 = Jan_2018 * (((1 + year_2018)^(1/12))-1) + Jan_2018) %>%
mutate (Mar_2018 = Feb_2018 * (((1 + year_2018)^(1/12))-1) + Feb_2018) %>%
mutate (Apr_2018 = Mar_2018 * (((1 + year_2018)^(1/12))-1) + Mar_2018) %>%
mutate (May_2018 = Apr_2018 * (((1 + year_2018)^(1/12))-1) + Apr_2018) %>%
mutate (Jun_2018 = May_2018 * (((1 + year_2018)^(1/12))-1) + May_2018) %>%
mutate (Jul_2018 = Jun_2018 * (((1 + year_2018)^(1/12))-1) + Jun_2018) %>%
mutate (Aug_2018 = Jul_2018 * (((1 + year_2018)^(1/12))-1) + Jul_2018) %>%
mutate (Sep_2018 = Aug_2018 * (((1 + year_2018)^(1/12))-1) + Aug_2018) %>%
mutate (Oct_2018 = Sep_2018 * (((1 + year_2018)^(1/12))-1) + Sep_2018) %>%
mutate (Nov_2018 = Oct_2018 * (((1 + year_2018)^(1/12))-1) + Oct_2018) %>%
mutate (Dec_2018 = Nov_2018 * (((1 + year_2018)^(1/12))-1) + Nov_2018) %>%
mutate (Jan_2019 = Dec_2018 * (((1 + year_2019)^(1/12))-1) + Dec_2018) %>%
mutate (Feb_2019 = Jan_2019 * (((1 + year_2019)^(1/12))-1) + Jan_2019) %>%
mutate (Mar_2019 = Feb_2019 * (((1 + year_2019)^(1/12))-1) + Feb_2019) %>%
mutate (Apr_2019 = Mar_2019 * (((1 + year_2019)^(1/12))-1) + Mar_2019) %>%
依此类推。输出看起来像
markets city traffic year_2018 year_2019 year_2020 Jan_2018 Feb_2018 Mar_2018 Apr_2018 May_2018 Jun_2018 Jul_2018 Aug_2018 Sep_2018 Oct_2018
1 AZ Bell 170105. 0.449 0.402 0.3262959 175440. 180943. 186618. 192472. 198509. 204735. 211157. 217780. 224611. 231656.
2 AZ Benson 1344. 0.449 0.402 0.3262959 1386. 1429. 1474. 1520. 1568. 1617. 1668. 1720. 1774. 1830.
3 AZ Buckeye 34155. 0.449 0.402 0.3262959 35227. 36331. 37471. 38646. 39859. 41109. 42398. 43728. 45100. 46514.
有没有一种有效的方法来计算每月的CAGR,因为我有直到2035年的数据(很抱歉没有发布全部,认为会非常庞大)。 我想获得一个包含市场,城市和每月CAGR值的新输出data_set,而不是添加到data_set_final中。 有人可以帮我吗。
答案 0 :(得分:0)
OP正在寻找累积产品,您可以使用cumprod
或仅使用(1+growthRate)^(seq_len(12)/12)
在基数R中进行此操作。
代码的中间和最长部分是格式化列名称,以获取所需的输出。
#calculate cagr
cagr <- do.call(rbind,
by(dat, paste(dat$markets, dat$city), function(datrow) {
rates <- datrow[, grepl("^year_", names(datrow))]
vals <- datrow[,"base"] * c(vapply(rates, function(x) (1+x)^(seq_len(12)/12), numeric(12)))
data.frame(t(vals))
})
)
#create the column names for cagr
years <- grep("^year_", names(dat), value=TRUE)
firstYr <- substr(years[1], nchar(years[1])-3, nchar(years[1]))
lastYr <- years[length(years)]
lastYr <- substr(lastYr, nchar(lastYr)-3, nchar(lastYr))
names(cagr) <- format(
seq(as.Date(paste0(firstYr, "-01-01")),
as.Date(paste0(lastYr, "-12-31")),
by="1 month"),
"%b_%Y")
#output
cagr
输出:
Jan_2018 Feb_2018 Mar_2018 Apr_2018 May_2018 Jun_2018 Jul_2018 Aug_2018 Sep_2018
AZ Bell 175440.03 180942.897 186618.368 192471.857 198508.946 204735.396 211157.144 217780.318 224611.24
AZ Benson 1385.93 1429.401 1474.236 1520.477 1568.169 1617.356 1668.086 1720.407 1774.37
AZ Buckeye 35226.58 36331.498 37471.075 38646.396 39858.582 41108.790 42398.212 43728.078 45099.66
Oct_2018 Nov_2018 Dec_2018 Jan_2019 Feb_2019 Mar_2019 Apr_2019 May_2019 Jun_2019
AZ Bell 231656.411 238922.567 246416.634 174961.613 179957.398 185095.831 190380.98 195817.047 201408.330
AZ Benson 1830.025 1887.426 1946.627 1382.151 1421.616 1462.208 1503.96 1546.903 1591.073
AZ Buckeye 46514.257 47973.227 49477.960 35130.517 36133.620 37165.365 38226.57 39318.076 40440.749
Jul_2019 Aug_2019 Sep_2019 Oct_2019 Nov_2019 Dec_2019 Jan_2020 Feb_2020 Mar_2020
AZ Bell 207159.264 213074.408 219158.450 225416.214 231852.660 238472.889 174154.973 178301.877 182547.526
AZ Benson 1636.504 1683.232 1731.294 1780.729 1831.575 1883.873 1375.779 1408.538 1442.078
AZ Buckeye 41595.478 42783.179 44004.793 45261.289 46553.662 47882.937 34968.552 35801.208 36653.691
Apr_2020 May_2020 Jun_2020 Jul_2020 Aug_2020 Sep_2020 Oct_2020 Nov_2020 Dec_2020
AZ Bell 186894.270 191344.517 195900.731 200565.436 205341.215 210230.713 215236.638 220361.76 225608.922
AZ Benson 1476.416 1511.571 1547.564 1584.414 1622.142 1660.767 1700.313 1740.80 1782.251
AZ Buckeye 37526.473 38420.038 39334.879 40271.505 41230.433 42212.194 43217.333 44246.41 45299.983
数据:
dat <- read.table(text="markets city base year_2018 year_2019 year_2020
AZ Bell 170104.516 0.448619 0.4019198 0.3262959
AZ Benson 1343.781 0.448619 0.4019198 0.3262959
AZ Buckeye 34155.261 0.448619 0.4019198 0.3262959", header=TRUE)
表明soln仍然适用于新数据:
dat <- read.table(text="markets city tla base year_2018 year_2019 year_2020 year_2021 year_2022 year_2023 year_2024 year_2025
1 AZ Bell BELL 170104.516 0.448619 0.4019198 0.3262959 0.25 0.25 0.25 0.25 0.25
2 AZ Benson BENS 1343.781 0.448619 0.4019198 0.3262959 0.25 0.25 0.25 0.25 0.25
3 AZ Buckeye BUCK 34155.261 0.448619 0.4019198 0.3262959 0.25 0.25 0.25 0.25 0.25", header=TRUE)
cagr输出:
Jan_2018 Feb_2018 Mar_2018 Apr_2018 May_2018 Jun_2018 Jul_2018 Aug_2018 Sep_2018 Oct_2018 Nov_2018 Dec_2018 Jan_2019 Feb_2019
AZ Bell 175440.03 180942.897 186618.368 192471.857 198508.946 204735.396 211157.144 217780.318 224611.24 231656.411 238922.567 246416.634 174961.613 179957.398
AZ Benson 1385.93 1429.401 1474.236 1520.477 1568.169 1617.356 1668.086 1720.407 1774.37 1830.025 1887.426 1946.627 1382.151 1421.616
AZ Buckeye 35226.58 36331.498 37471.075 38646.396 39858.582 41108.790 42398.212 43728.078 45099.66 46514.257 47973.227 49477.960 35130.517 36133.620
Mar_2019 Apr_2019 May_2019 Jun_2019 Jul_2019 Aug_2019 Sep_2019 Oct_2019 Nov_2019 Dec_2019 Jan_2020 Feb_2020 Mar_2020 Apr_2020
AZ Bell 185095.831 190380.98 195817.047 201408.330 207159.264 213074.408 219158.450 225416.214 231852.660 238472.889 174154.973 178301.877 182547.526 186894.270
AZ Benson 1462.208 1503.96 1546.903 1591.073 1636.504 1683.232 1731.294 1780.729 1831.575 1883.873 1375.779 1408.538 1442.078 1476.416
AZ Buckeye 37165.365 38226.57 39318.076 40440.749 41595.478 42783.179 44004.793 45261.289 46553.662 47882.937 34968.552 35801.208 36653.691 37526.473
May_2020 Jun_2020 Jul_2020 Aug_2020 Sep_2020 Oct_2020 Nov_2020 Dec_2020 Jan_2021 Feb_2021 Mar_2021 Apr_2021 May_2021 Jun_2021
AZ Bell 191344.517 195900.731 200565.436 205341.215 210230.713 215236.638 220361.76 225608.922 173297.253 176549.915 179863.627 183239.535 186678.807 190182.631
AZ Benson 1511.571 1547.564 1584.414 1622.142 1660.767 1700.313 1740.80 1782.251 1369.003 1394.698 1420.875 1447.544 1474.714 1502.393
AZ Buckeye 38420.038 39334.879 40271.505 41230.433 42212.194 43217.333 44246.41 45299.983 34796.330 35449.432 36114.791 36792.640 37483.210 38186.743
Jul_2021 Aug_2021 Sep_2021 Oct_2021 Nov_2021 Dec_2021 Jan_2022 Feb_2022 Mar_2022 Apr_2022 May_2022 Jun_2022 Jul_2022 Aug_2022
AZ Bell 193752.219 197388.81 201093.648 204868.028 208713.25 212630.645 173297.253 176549.915 179863.627 183239.535 186678.807 190182.631 193752.219 197388.81
AZ Benson 1530.592 1559.32 1588.587 1618.404 1648.78 1679.726 1369.003 1394.698 1420.875 1447.544 1474.714 1502.393 1530.592 1559.32
AZ Buckeye 38903.480 39633.67 40377.564 41135.422 41907.50 42694.076 34796.330 35449.432 36114.791 36792.640 37483.210 38186.743 38903.480 39633.67
Sep_2022 Oct_2022 Nov_2022 Dec_2022 Jan_2023 Feb_2023 Mar_2023 Apr_2023 May_2023 Jun_2023 Jul_2023 Aug_2023 Sep_2023 Oct_2023
AZ Bell 201093.648 204868.028 208713.25 212630.645 173297.253 176549.915 179863.627 183239.535 186678.807 190182.631 193752.219 197388.81 201093.648 204868.028
AZ Benson 1588.587 1618.404 1648.78 1679.726 1369.003 1394.698 1420.875 1447.544 1474.714 1502.393 1530.592 1559.32 1588.587 1618.404
AZ Buckeye 40377.564 41135.422 41907.50 42694.076 34796.330 35449.432 36114.791 36792.640 37483.210 38186.743 38903.480 39633.67 40377.564 41135.422
Nov_2023 Dec_2023 Jan_2024 Feb_2024 Mar_2024 Apr_2024 May_2024 Jun_2024 Jul_2024 Aug_2024 Sep_2024 Oct_2024 Nov_2024 Dec_2024
AZ Bell 208713.25 212630.645 173297.253 176549.915 179863.627 183239.535 186678.807 190182.631 193752.219 197388.81 201093.648 204868.028 208713.25 212630.645
AZ Benson 1648.78 1679.726 1369.003 1394.698 1420.875 1447.544 1474.714 1502.393 1530.592 1559.32 1588.587 1618.404 1648.78 1679.726
AZ Buckeye 41907.50 42694.076 34796.330 35449.432 36114.791 36792.640 37483.210 38186.743 38903.480 39633.67 40377.564 41135.422 41907.50 42694.076
Jan_2025 Feb_2025 Mar_2025 Apr_2025 May_2025 Jun_2025 Jul_2025 Aug_2025 Sep_2025 Oct_2025 Nov_2025 Dec_2025
AZ Bell 173297.253 176549.915 179863.627 183239.535 186678.807 190182.631 193752.219 197388.81 201093.648 204868.028 208713.25 212630.645
AZ Benson 1369.003 1394.698 1420.875 1447.544 1474.714 1502.393 1530.592 1559.32 1588.587 1618.404 1648.78 1679.726
AZ Buckeye 34796.330 35449.432 36114.791 36792.640 37483.210 38186.743 38903.480 39633.67 40377.564 41135.422 41907.50 42694.076