I have data about the population of every state over time. For each row in this dataframe, I want to add an avg_pop
column that is the average population of that state over all time periods. How can I achieve that in R?
Example:
st1 10
st1 20
should become
st1 10 15
st1 10 15
Because the average across st1 is 15.
I tried this but it does not work because the size of the dataframes is different:
averages = aggregate(data, list(data$state_name), mean, na.rm=T)
data$avg_pop = subset(averages, state_name==data$state_name)$stpop