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")