Question
Custom function with dplyr::summarise with conditions
I want to create a function named ratio_function that does the same as the following code:
data = data %>%
group_by(ID) %>%
summarise(sum_ratio = sum(surface[category == "A"], na.rm = T)/sum(total_area[category == "A"], na.rm = T)*mean(`MEAN`[category == "A"], na.rm = T))
but inside of summarise such as:
data = data %>%
group_by(ID) %>%
summarise(sum_ratio = ratio_function("A"))
The problem is that surface, total_area and category aren't recognized as variable name in summarise once they are called in the function.