Calculate mean age for population from two columns


I am brand new to R, I need to calculate the mean age for an entire population. I have filtered my data frame down to two columns 'Age' and 'Population' and it looks like this:

Age Population
0 - 10,178
1 - 22,826
2 - 15,836
3 - 27,196
4 - 32,105

The age range is 0-55, and I am not clear on how to best calculate this.

mean(df$age) just give me the mean of the 0-55, ie 27.5, so not sure how to bring in the population values.

Really appreciate any guidance on this, I am getting the feeling I am not approaching this from the right angle.


This is called a weighted mean, and there's actually a function in R for it, weighted.mean(). This StackOverflow thread has some examples:

1 Like