# Code to illustrate how to calculate mean of a histogram (possibly # with uneven class-widths). # Example data: # As input we use a frequency distribution of age groups, stored in two variables: # nr = the counts per age group # agegrp = the labels for each age group nr <- c(8, 11, 26, 20, 13, 5) agegrp <- c('<1','1-2','3-5','6-11','12-17','18+') # Based on the variable agegrp, the mean age of each age-group is # determined. The result is stored in the variable midgrp. # The mean age is just the middle value of the range - here done by # 'hand-work' (no programming). # For the highest interval, the assumption was made that it ranges up to 20. midgrp <- c(0.5, 1.5, 4, 8.5, 14.5, 19) # Now the estimated mean age for this frequency distribution # is calculated by multiplying the mean per interval with the reltive frequency # per interval and summing these values. relfreq <- nr/sum(nr) ( mean <- sum(midgrp * relfreq) )