I want to add a mean of Temp per month as a column to the airquality dataset. So, I want something like this:
Ozone Solar.R Wind Temp Month Day NEW COLUMN
41 190 7.4 67 5 1 77.9
36 118 8 72 5 2 77.9
12 149 12.6 74 5 3 77.9
18 313 11.5 62 5 4 77.9
NA NA 14.3 56 5 5 77.9
28 NA 14.9 66 5 6 77.9
Where the new column is a mean of Temp/month. So, it will repeat the mean of Temp in the rows where Month=5, then another mean of Temp where Month=6 etc.
I've tried this:
airquality %>% mutate(col = sapply(split(Temp, Month), min))
But I get an error saying that this renders 5 rows, while my dataframe has 153.
How do I solve this in an elegant way?