How to calculate new column depending on aggregate function on group using dplyr (add summary statistics on the summary statistics)?
Quite often I need to calculate a new column for an R dataframe (in long form), whose value should depend on an aggregate function (e.g. sum) of a group. For instance, I might want to know what fraction of sales a product accounts for on any given day: daily fraction = revenue for product i on day d / sum or revenue for all products on day d My current strategy is to summarise and join: library(dplyr) join_summary <- function(data, ...) left_join(data, summarise(data, ...)) data = data.frame( day = c(1,1,2,2,3,3), product = rep(c("A", "B"), 3), revenue = c(2, 4, 8, 7, 9, 2) ) data2 <- data %>%