How to pass the result of a variable in the first summarize to the second summarize?

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)

I just figured it out myself again...LOL

just need to add as.data.frame() after f and I forget to add return() in the function, which also causes the problem...

hope it helps

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}})) %>% as.data.frame()

g<-merge(periods, f) %>% 
  mutate(new_AWP=total_AWP*10) %>% 
  group_by(p, start,end,Provider, Network) %>% 
  summarize(new_awp=sum(new_AWP))
return(g)
}

cal_awp(data = AWP)  
#> # A tibble: 9 x 6
#> # Groups:   p, start, end, Provider [9]
#>       p start      end        Provider Network new_awp
#>   <int> <date>     <date>     <chr>    <chr>     <dbl>
#> 1     1 2020-01-01 2020-12-31 a        50k       35000
#> 2     1 2020-01-01 2020-12-31 b        45k       35000
#> 3     1 2020-01-01 2020-12-31 c        40k       35000
#> 4     2 2021-01-01 2021-12-31 a        50k       35000
#> 5     2 2021-01-01 2021-12-31 b        45k       35000
#> 6     2 2021-01-01 2021-12-31 c        40k       35000
#> 7     3 2022-01-01 2022-12-31 a        50k       35000
#> 8     3 2022-01-01 2022-12-31 b        45k       35000
#> 9     3 2022-01-01 2022-12-31 c        40k       35000

Created on 2020-03-18 by the reprex package (v0.3.0)

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.