I'm looking for a quicker way to summarize by group type, for many different groups in one df without having to subset. Below is a sample data frame and the current code I use to accomplish it. It seems verbose to me, and I'm guessing there is a quicker way to resolve this. In this example, my code summarizes health revenue grouped by name, then merges it back into the master. I'd like to summarize both health and vision variables, grouped by name. The key, is that I only want revenues for health and vision, when there is a 1 in the variable. Thanks for your help.
#df name = c("jerry","jerry","jerry","dave","dave","dave","mary","mary","mary") health = c(1,0,1,1,0,1,0,1,1) vision = c(0,1,0,0,1,0,1,0,0) rev =c(100,200,500,1000,800,300,400,600,300) df = data.frame(name,health,vision,rev) #Subset health health = subset(df, health == 1) #Sum by group type library(dplyr) health <- health %>% group_by(name) %>% mutate( health_rev=sum(rev, na.rm = TRUE)) #Select variables health <- health[c("name","health_rev")] #Remove duplicates health <- health[!duplicated(health$name), ] #Merge back to master master <- merge(x = df, y = health, by = "name", all.x = TRUE)