In R, I have a bunch of data in a dataframe like:
state | zip | value
______|_______|______
CA | 94555 | 18
CA | 94556 | 5
OH | 12345 | 22
OH | 12346 | 10
and so on.
I want an add a column to each row listing the mean 'value' for that state.
I can get a dataframe of the means via "(aggregate(data$value, list(State = data$state), mean))
". That gives me a dataframe with 50 rows, one for each state. But I need to then go back into the original dataframe and put the state's average in rows belonging to that state.
How would I go about doing this?