R中基于柱标题的自动CAGR计算

时间:2013-10-01 22:58:55

标签: r function loops data.table

我有一个数据集,其中包含多个品牌和年份的收入数据。每个列名都有Brandname.yr,用于09-12。有些品牌的数据仅为10-12或11-12。我开发了以下代码来计算每个品牌的每个客户的CAGR。但是当我执行代码时,我的CAG成千上万(即4800.74)。有没有人对CAGR计算没有正确执行的原因有任何建议?

数据示例。

ClientID    Rv.Brand1.09    Rv.Brand1.10    Rv.Brand1.11    Rv.Brand1.12    Rv.Brand2.09    Rv.Brand2.10    Rv.Brand2.11    Rv.Brand2.12
1   6991979 6931508 5071305 4944208 2079843 2990803 2111142 1977724
2   0   0   0   0   0   0   0   0
3   0   29425   0   0   0   29425   0   0
4   0   0   0   0   0   0   0   0
5   0   0   0   0   0   0   0   0




library(data.table)
dataset <- data.table(mdb)
# Getting the list of column names for which CAGR needs to be calculated
Instance09 = gsub(
  colnames(dataset)[
    grepl(colnames(dataset), pattern = ".09")
    ], 
  pattern = ".09", 
  replacement = ""
)

Instance10 = gsub(
  colnames(dataset)[
    grepl(colnames(dataset), pattern = ".10")
    ], 
  pattern = ".10", 
  replacement = ""
)

Instance11 = gsub(
  colnames(dataset)[
    grepl(colnames(dataset), pattern = ".11")
    ], 
  pattern = ".11", 
  replacement = ""
)

Instance12 = gsub(
  colnames(dataset)[
    grepl(colnames(dataset), pattern = ".12")
    ], 
  pattern = ".12", 
  replacement = ""
)

Instance0912 <- intersect(Instance09,Instance12)

Instance1012 <- intersect(Instance10,Instance12)

Instance1112 <- intersect(Instance11,Instance12)

Instance1012 <- Instance1012[!Instance1012 %in% Instance0912]

Instance1112 <- Instance1112[!Instance1112 %in% Instance0912]

for ( i in Instance0912 )
{
  #calculating CAGR for each i
  #dataset is a data.table and not a data.frame
  dataset[, 
          paste0("CAGR",i):= (get(paste0(i,".12")) / get(paste0(i,".09")) ^ (1/3)) - 1
          ]

}

for ( i in Instance1012 )
{
  #calculating CAGR for each i
  #dataset is a data.table and not a data.frame
  dataset[, 
          paste0("CAGR",i):= (get(paste0(i,".12")) / get(paste0(i,".10")) ^ (1/2)) - 1
          ]

}

for ( i in Instance1112 )
{
  #calculating CAGR for each i
  #dataset is a data.table and not a data.frame
  dataset[, 
          paste0("CAGR",i):= (get(paste0(i,".12")) / get(paste0(i,".11")) ^ 1) - 1
          ]

}

1 个答案:

答案 0 :(得分:0)

在计算CAGR时需要额外设置一组括号,如第一种情况所示:

for ( i in Instance0912 )
{
#calculating CAGR for each i
#dataset is a data.table and not a data.frame
dataset[, 
        paste0("CAGR",i) := ((get(paste0(i,".12")) / get(paste0(i,".09"))) ^ (1/3)) - 1
        ]

}

你应该小心grepl(),在正则表达式中,点.匹配任何字符。您需要将其写为\.以便按字面匹配点。在具体情况下不是问题,只要注意它。

我还建议考虑以长格式重组数据,并可能定义一个计算CAGR的函数(以避免在不同月份拼写相同计算时出现技术错误。