Advertisement
vanloon

calculate mean from histogram

May 25th, 2024
640
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 0.95 KB | None | 0 0
  1. # Code to illustrate how to calculate mean of a histogram (possibly
  2. # with uneven class-widths).
  3.  
  4. # Example data:
  5. # As input we use a frequency distribution of age groups, stored in two variables:
  6. #   nr = the counts per age group
  7. #   agegrp = the labels for each age group
  8.  
  9. nr <- c(8, 11, 26, 20, 13, 5)
  10. agegrp <- c('<1','1-2','3-5','6-11','12-17','18+')
  11.  
  12. # Based on the variable agegrp, the mean age of each age-group is
  13. # determined. The result is stored in the variable midgrp.
  14. # The mean age is just the middle value of the range - here done by
  15. # 'hand-work' (no programming).
  16. # For the highest interval, the assumption was made that it ranges up to 20.
  17. midgrp <- c(0.5, 1.5, 4, 8.5, 14.5, 19)
  18.  
  19. # Now the estimated mean age for this frequency distribution
  20. # is calculated by multiplying the mean per interval with the reltive frequency
  21. # per interval and summing these values.
  22. relfreq <- nr/sum(nr)
  23. ( mean <- sum(midgrp * relfreq) )
  24.  
  25.  
Tags: mean histogram
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement