[Solved] Calculate conditional mean in R with dplyr (like group by in SQL) [duplicate]
I think what you are looking for (if you want to use dplyr) is a combination of the functions group_byand mutate. library(dplyr) city <- c(“a”, “a”, “b”, “b”, “c”) temp <- 1:5 df <- data.frame(city, temp) df %>% group_by(city) %>% mutate(mean(temp)) Which would output: city temp mean(temp) (fctr) (int) (dbl) 1 a 1 1.5 2 … Read more