Hey guys,
I'm new to R. Recently I came across a problem and I can't figure it out. I wonder how I can pass a numeric variable to summarize in a function? More specific, I want to group by "Provider" and "Network" and summarize "AWP" and "Claim".
I write a simple function to illustrate my problem. In this case, I got an error when I run the function
cal_var("AWP") or cal_var("Claim").
my error is: "Error in sum(var) : invalid 'type' (character) of argument ". Thank you so much for your help!
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_var<-function(var){
example %>%
group_by(Provider, Network) %>% summarize(total_var= sum(var))
}
cal_awp("AWP")
cal_awp("Claim")