Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
---
title: "Massachusetts Data by Zip Code"
output: html_document
---
```{r setup, include=FALSE}
### This R Markdown document needs a separate .rdata file in the same directory
knitr::opts_chunk$set(echo = FALSE)
if(!require("pacman")){
install.packages("pacman")
}
pacman::p_load(DT, dplyr, ggplot2, scales)
load("mamarkdowndata.rdata") # or whatever you called your file storing data
```
# Find highest and lowest median incomes. Ignores ties for code simplicity.
```{r}
zip_highest_income_row <- filter(markdowndata, MedianHouseholdIncome == max(MedianHouseholdIncome, na.rm = TRUE))
zip_lowest_income_row <- filter(markdowndata, MedianHouseholdIncome == min(MedianHouseholdIncome, na.rm = TRUE))
```
```
Zip code `r zip_highest_income_row$ZipCode[1]` in `r zip_highest_income_row$City[1]` has the highest median income in Massachusetts, `r scales::dollar(zip_highest_income_row$MedianHouseholdIncome[1])`.
Zip code `r zip_lowest_income_row$ZipCode[1]` in `r zip_lowest_income_row$City[1]` has the lowest median income in Massachusetts, `r scales::dollar(zip_lowest_income_row$MedianHouseholdIncome[1])`.
#### Distribution of median household incomes
```{r histo, fig.width = 4, fig.height = 2, warning=FALSE, message=FALSE}
ggplot(markdowndata, aes(x = MedianHouseholdIncome)) +
geom_histogram(binwidth = 10000, color = "black", fill = "darkgreen") +
theme_classic() +
xlab("") + ylab("") +
scale_x_continuous(labels = dollar)
```
#### Income, Housing Costs, and Population
```{r table}
DT::datatable(markdowndata, filter = 'top') %>%
formatCurrency(4:5, digits = 0) %>%
formatCurrency(6, currency = "", digits = 0)
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.