1

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
shurup
  • 751
  • 10
  • 33

2 Answers2

2

If we want to create a column, use ave

data$avg_pop <- with(data, ave(col_name, state_name))
akrun
  • 874,273
  • 37
  • 540
  • 662
1

To calculate a new column, you can use the mutate function:

library(tidyverse)

df %>% mutate(avg_pop = mean(mass, na.rm = TRUE))