0

I am using the deaths file from chapters 5.2 and 5.3 of the Mastering Metrics data that can be found here. I am trying to group pop by state and year and return the mean pop. I have tried several methods but when I merge the group the results do not look proper.

mlda <- read.dta("C:/Users/perdue/Desktop/Adv.MicroEconometrics/HA 8/deaths.dta") ## reading in my data
mlda$avg.pop <- aggregate(mlda[, 7], list(mlda$state), mean)

Error in `$<-.data.frame`(`*tmp*`, avg.pop, value = list(Group.1 = c(1L,  : 
  replacement has 51 rows, data has 24786
Collective Action
  • 7,607
  • 15
  • 45
  • 60
  • 1
    The `aggregate` returns a summarised output i.e. the number of rows will be lesser than the original dataset. You need `ave` from `base R`ie. `mlda$avg.pop <- with(mlda, ave(yourcolumn, state))` Or use `mutate` `library(dplyr); mlda %>% group_by(state) %>% mutate(avg.pop = mean(yourcolumn))` – akrun Jan 15 '18 at 07:30
  • 1
    @akrun: Yes, that totally worked!!! – Collective Action Jan 15 '18 at 07:36

0 Answers0