January 17, 2018

Introduction

Data Preparation

The dataset (the population csv) is reformatted for further processing in R:

  • Extract the columns E through H and rename to:
    • State, Population, Popu18andUp (population of age 18 and older), and Popu18Pct (percentage of population >= age 18)
  • Change Popu18Pct to reflect the real values by dividing by 100.

  • Add the Code column for US State 2-character codes.

  • Lastly, copy 2016 population numbers (columns B of the population change table) and paste into the csv.

  • Calculate the population changes and the % of the changes.

  • Write the reformatted data frame to USpopstates.csv.

Data Preparation Code

library(dplyr); library(xlsx)
df = read.csv("https://www2.census.gov/programs-surveys/popest/datasets/2010-2017/state/asrh/scprc-est2017-18+pop-res.csv")
df = df[2:52,5:8] # remove the first and the last rows
statecodes = append(state.abb,"DC",8)
df = df %>% mutate(Code=statecodes) 
names(df) = c("State","Population","Popu18andUp","Popu18Pct","Code")
df = df %>%  mutate(Popu18Pct = Popu18Pct/100)
download.file(
   "https://www2.census.gov/programs-surveys/popest/tables/2010-2017/state/totals/nst-est2017-03.xlsx",
   "popuchangetable.xlsx",mode="wb")
table = read.xlsx("popuchangetable.xlsx", header=FALSE, startRow=11, endRow=61, 
                  sheetName="NST03")
popu2016.tbl = table$X2  # 2nd column hosts the 2016 population numbers

df = df %>% mutate(Popu2016 = popu2016.tbl) %>% mutate(PopuChg = Population - Popu2016) %>%
            mutate(PctChg = PopuChg / Popu2016)
write.csv(df,"USpopstates.csv")

Shiny Application

  • The application shows 3 maps in 3 tabs on the main pane.
    • US Population by State
    • The % of US Population Changes since July 2016
    • The % of US Population of Age 18 and older
  • The stats of each state show on hover.

  • On the left pane, Enter a valid state code (i.e. NY for New York) to see the stats related to the state.

  • The links to the data source and the estimate methodology employed by the US Census Bureau are located on the left pane as well.

  • The ui.R and server.R files are located at GitHub.