Hi there,
I was trying to create a function as below. In this case, my variable is called data, which is the "AWP" in the example data frame. I wonder how I can pass the result of my variable (total_AWP) in the first summarize to the second summarize after a mutate? I need the result from the second summarize.
Hope the question is clear. Many thanks!
library(rlang)
library(dplyr, warn.conflicts = FALSE)
periods <- data.frame(p = c(1:3), start = c(as.Date('2020-01-01', '%Y-%m-%d'), as.Date('2021-01-01', '%Y-%m-%d'),as.Date('2022-01-01', '%Y-%m-%d')),
end = c(as.Date('2020-12-31', '%Y-%m-%d'), as.Date('2021-12-31', '%Y-%m-%d'),as.Date('2022-12-31', '%Y-%m-%d')))
example<-data.frame("Provider" = c("a", "b", "c", "c", "b", "a"), "Network" = c("50k", "45k", "40k", "40k", "45k", "50k"),
"AWP" = c(500, 1000, 1500, 2000, 2500, 3000), "Claim" = c(100, 150, 200, 250, 300, 350), stringsAsFactors = FALSE)
cal_awp<-function(data){
f<-example %>%
group_by(Provider, Network) %>% summarize(total_AWP = sum({{data}}))
g<-merge(periods, f) %>%
mutate(new_AWP=total_AWP*10) %>%
group_by(p, start,end,Provider, Network) %>%
summarize(new_awp=sum(new_AWP))
}
cal_awp(data = AWP)
Created on 2020-03-18 by the reprex package (v0.3.0)