Skip to content

Instantly share code, notes, and snippets.

@emitanaka
Last active June 11, 2020 03:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emitanaka/97e8973700a149c56c85ba3db81ef25b to your computer and use it in GitHub Desktop.
Save emitanaka/97e8973700a149c56c85ba3db81ef25b to your computer and use it in GitHub Desktop.
Median income calculation from range
library(tidyverse)
income_df <- tibble(income_range = c("0-74", "75-145", "150-325", "325+"),
count = c(125, 170, 215, 250)) %>%
separate(income_range,
into = c("income_min", "income_max"),
convert = TRUE)
calc_median_income <- function(df) {
df %>%
arrange(income_min) %>%
mutate(cumperc = cumsum(count) / sum(count)) %>%
# subset to two rows that are just below cumperc >= 0.5 and above <=0.5
slice((which.min(cumperc < 0.5) - 1):which.max(cumperc > 0.5)) %>%
# do a linear interpolation to find the median minimum to maximum income
summarise(median_min_income = approx(cumperc, income_min, xout = 0.5)$y,
median_max_income = approx(cumperc, income_max, xout = 0.5)$y)
}
calc_median_income(income_df)
#> # A tibble: 1 x 2
#> median_min_income median_max_income
#> <dbl> <dbl>
#> 1 105. 216.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment