Skip to content

Instantly share code, notes, and snippets.

@njtierney
Created June 1, 2020 02:58
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 njtierney/d679a310e9d0258175e29c9f711d92ff to your computer and use it in GitHub Desktop.
Save njtierney/d679a310e9d0258175e29c9f711d92ff to your computer and use it in GitHub Desktop.
library(tidyverse)
income <- tibble(income_range = c("0-74",
                                  "75-145",
                                  "150-325",
                                  "325+"),
                 count = c(125,
                           170, 
                           215,
                           250))

# requires dplyr 1.0.0
# install.packages()
income %>% 
  separate(col = income_range,
           into = c("lower", "upper")) %>% 
  mutate(lower = as.numeric(lower),
         upper = as.numeric(upper)) %>% 
  rowwise() %>% 
  mutate(med = median(c(lower, upper), na.rm = TRUE))
#> # A tibble: 4 x 4
#> # Rowwise: 
#>   lower upper count   med
#>   <dbl> <dbl> <dbl> <dbl>
#> 1     0    74   125   37 
#> 2    75   145   170  110 
#> 3   150   325   215  238.
#> 4   325    NA   250  325

Created on 2020-06-01 by the reprex package (v0.3.0)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment