I'd like to add a column to my data set that is populated with the averages the values of a given category.
Using the iris data set as an example
library(datasets)
head(iris)
unique(iris$Species)
mutate(iris, spec_avg_pet_width=1)
I'd like to replace "1" with the average of virginica, setosa and versicolor corresponding correctly to the Species of that row. I will ultimately be using this average as a way to arrange categories in a ggplot. The excel equivalent of this would be an AverageIfs function.
I know how to do a conditional mean based on an absolute value"
mean(iris[iris$Species =='setosa','Petal.Width'])
But cannot figure out how to do a conditional mean based on a relative, corresponding value.
Essentially I would like my new column to return the values of 0.246, 2.026 or 1.326 depending on whether the row is setosa, virginica or versicolor.
Thank you!