Given the df
ww <- data.frame(
GM = c("A", "A", "A", "A", "A", "A",
"B", "B", "B", "B", "B", "B",
"C", "C", "C", "C", "C", "C"),
stanza = rep(c("Past", "Mid", "End"), 6),
change = c(1, 1.1, 1.4, 1, 1.3, 1.5, 1, 1.2, 1.4,
1.1, 1.2, 1.3, .9, 1.2, 1.3, .9, 1.3, 1.5))
I would like to calculate the mean for Past for each GM and the divide each value in 'change' by the GM specific mean. I can do this with two dplyr calls and a join function as follows:
past <- ww %>%
group_by(GM) %>%
filter(stanza == "Past") %>%
summarize(past.mean = mean(change))
ww <- left_join(ww, past, by = "GM")
ww %>%
group_by(GM, stanza) %>%
summarize(pr.change = change/past.mean)
But there must be a way to do this in one dplyr call.