I try to write a simple function to obtain the rate between columns in a dataframe on a aggregated level. I would like to obtain the same output as the output obtained by:
library(dplyr)
set.seed(1)
dat <- data.frame(x = rep(1:3, each = 5), a = runif(15, 0, 1), b = runif(15, 0, 2))
oper_fn <- function(df, oper){
oper <- enquo(oper)
df %>%
group_by(x) %>%
summarize(output = !! oper) %>%
ungroup()
}
oper_fn(dat, sum(a) / sum(b))
The following should also work:
oper_fn(dat, sum(a))
What is the way to do this in base R?