根据条件

时间:2018-02-10 01:09:36

标签: r dplyr

我正在使用此数据框:

 structure(list(year = c("2012", "2016", "2012", "2016"), month = c("12", 
"12", "12", "12"), company = c("ALSN", "ALSN", "DAN", "DAN"), 
    Revenue = c(2141.8, 1840.2, 7224, 5826), `Cost of Goods Sold` = c(1187.5, 
    976, 6250, 4982), `Gross Profit` = c(954.3, 864.2, 974, 844
    ), `Gross Margin %` = c(44.56, 46.96, 13.48, 14.49), `Selling, General, & Admin. Expense` = c(419, 
    323.9, 424, 406), `Impairment Of Capital Assets` = c(0, 0, 
    2, 0), Advertising = c(1, 1, 1, 1), `Research & Development` = c(115.1, 
    88.8, 0, 0), `Restructuring And Mergern Acquisition` = c(0, 
    0, 47, 0), `Other Operating Expense` = c(-5.68434188608e-14, 
    1.13686837722e-13, 121, 8), `Operating Income` = c(420.2, 
    451.5, 429, 430), `Operating Margin %` = c(19.62, 24.54, 
    5.94, 7.38), `Interest Income` = c(0.9, 0.7, 24, 13), `Interest Expense` = c(-152.1, 
    -101.6, -84, -113), `Net Interest Income` = c(-151.2, -100.9, 
    -60, -100), `Other Income (Expense)` = c(-52.8, -9.3, -5, 
    -115), `Non Operating Income` = c(-52.8, -9.3, -5, -115), 
    `Other Income (Minority Interest)` = c(0, 0, -15, -13), `Gain on Sale of Security` = c(-1.3, 
    -0.8, 0, 7), `Write Off` = c(1, 1, 1, 1), `Pre-Tax Income` = c(216.2, 
    341.3, 364, 215), `Tax Provision` = c(298, -126.4, -51, 424
    ), `Tax Rate %` = c(-137.84, 37.03, 14.01, -197.21), `Net Income (Continuing Operations)` = c(514.2, 
    214.9, 315, 653), `Net Income (Discontinued Operations)` = c(0, 
    0, 0, 0), `Net Income` = c(514.2, 214.9, 300, 640), `Net Margin %` = c(24.01, 
    11.68, 4.15, 10.99), `Preferred Dividends` = c(0, 0, 31, 
    0), `EPS (Basic)` = c(2.83, 1.28, 1.82, 4.38), `EPS (Diluted)` = c(2.76, 
    1.27, 1.4, 4.36), `Shares Outstanding (Diluted Average)` = c(186.2, 
    168.8, 214.7, 146.8), `Depreciation, Depletion and Amortization` = c(252.5, 
    175.9, 277, 182), EBITDA = c(620.8, 618.8, 725, 510)), .Names = c("year", 
"month", "company", "Revenue", "Cost of Goods Sold", "Gross Profit", 
"Gross Margin %", "Selling, General, & Admin. Expense", "Impairment Of Capital Assets", 
"Advertising", "Research & Development", "Restructuring And Mergern Acquisition", 
"Other Operating Expense", "Operating Income", "Operating Margin %", 
"Interest Income", "Interest Expense", "Net Interest Income", 
"Other Income (Expense)", "Non Operating Income", "Other Income (Minority Interest)", 
"Gain on Sale of Security", "Write Off", "Pre-Tax Income", "Tax Provision", 
"Tax Rate %", "Net Income (Continuing Operations)", "Net Income (Discontinued Operations)", 
"Net Income", "Net Margin %", "Preferred Dividends", "EPS (Basic)", 
"EPS (Diluted)", "Shares Outstanding (Diluted Average)", "Depreciation, Depletion and Amortization", 
"EBITDA"), row.names = c(NA, 4L), class = "data.frame")

常量:

startDate <- "2012-01-01"
endDate <- "2016-12-31"

我想要的:创建一个将自定义函数应用于所有数字列的函数。我正在尝试计算复合年增长率。 CAGR公式如下:

((结束值/起点值)^(1 /年数)-1)

正如您所看到的,我需要每列能够找到正确的结束值和起始值。

我现在的功能就是:

cagr <- function(startval,endval,x,y,years){
  return(((endval[x == year(endDate)]/startval[y == year(startDate)])^(1/(years-1)))-1)
}

cagrNew <- function(df,colum,x,y,years){
  colum <- quo(colum)
  x <- quo(x)
  y <- quo(y)
  out <- df %>%
    group_by(!!company) %>%
    summarise(xxxx = cagr(!!colum[!!x == year(endDate)],!!colum[!!y == year(startDate)],!!x,!!y,numYears))
  return(out)
}

当我运行上述功能(cagrNEW)时,我收到此错误:

Error in `[.formula`(colum, !(!x == year(endDate))) : 
  attempt to set an attribute on NULL 

我想要的输出:

Company    RevenueCagr     Cost of Goods Sold CAGR ....
ALSN          .5%             .3%
DAN           .3%             .2%

1 个答案:

答案 0 :(得分:1)

我没有重复上面的数据,以节省空间。转换为tibble并指定。

# df <- as_tibble(...)


library(tidyverse)
library(scales) #< For percentage formatting

start_year <- 2012
end_year <- 2016

df %>% 
  filter(year %in% c(start_year, end_year)) %>% 
  group_by(company) %>% 
  arrange(desc(year), .by_group = TRUE) %>% 
  summarise_if(is.double, funs(CAGR =  percent( (.[[1]]/.[[2]])^ (1/(end_year - start_year)) - 1) )  ) 
                      # CAGR = ((End Value / Beginning Value)^(1/number of years)-1)

#Checksum: ALSN Company, Revenue
# (End Value / Beginning Value)^((1/number of years))-1 
percent(( (1840      / 2142)           ^ (1/(2016-2012))    - 1))
#> [1] "-3.73%"