-
Notifications
You must be signed in to change notification settings - Fork 0
/
ACS_2014_5YR.R
75 lines (63 loc) · 2.68 KB
/
ACS_2014_5YR.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# install packages and connect to the ACS api
install.packages ('leaflet')
install.packages ('acs')
install.packages('dplyr')
library(leaflet)
library(acs)
library(dplyr)
api_key = 'fd03623f08e6dad5ec49d40d699ee55ab4429cc4'
acs::api.key.install(key = api_key)
# How to look up which tables to bring in. Find table numbers from metadata excel file
acs.lookup(2014, span =5, dataset = "acs", table.number = "B01002")
# Bring in data by Urban Area
census_data <-
acs::acs.fetch(
endyear = 2014,
span = 5,
geography = acs::geo.make(
urban.area = '*'
),
variable = c('B19013_001','B01002_001', 'B19080_001', 'B19080_002','B19080_003', 'B19080_004', 'B02001_001', 'B02001_002', 'B03003_003', 'B02001_003', 'B02001_005', 'B02001_008', 'C15010_001', "B23025_005", "B23025_007", "B19083_001", "B25064_001")
)
#Create data frame
demographic <-
dplyr::data_frame(
STATEFP = census_data@geography$NAME,
UrbanAreaNo = census_data@geography$urbanarea,
income = census_data@estimate %>% data.frame %>% .[,1],
age = census_data@estimate %>% data.frame %>% .[,2],
income.LowestQuintile = census_data@estimate %>% data.frame %>% .[,3],
income.SecondQuintile = census_data@estimate %>% data.frame %>% .[,4],
income.ThirdQuintile = census_data@estimate %>% data.frame %>% .[,5],
income.FourthQuintile = census_data@estimate %>% data.frame %>% .[,6],
pop = census_data@estimate %>% data.frame %>% .[,7],
white = census_data@estimate %>% data.frame %>% .[,8],
hispanic = census_data@estimate %>% data.frame %>% .[,9],
africanamerican = census_data@estimate %>% data.frame %>% .[,10],
asian = census_data@estimate %>% data.frame %>% .[,11],
twoormoreraces = census_data@estimate %>% data.frame %>% .[,12],
educated = census_data@estimate %>% data.frame %>% .[,13],
unemployed = census_data@estimate %>% data.frame %>% .[,14],
notworkforce = census_data@estimate %>% data.frame %>% .[,15],
gini = census_data@estimate %>% data.frame %>% .[,16],
rent = census_data@estimate %>% data.frame %>% .[,17]
#Clean Data
) %>%
dplyr::mutate(
STATEFP = gsub("\\(2010\\)", "", STATEFP),
STATEFP = gsub(" Urban Cluster ", "", STATEFP),
STATEFP = gsub(" Urbanized Area ", "", STATEFP),
State = gsub("^.*\\,", "", STATEFP),
UrbanArea = gsub(",.*", "",STATEFP),
pct_white = white/pop,
pct_africanamerican = africanamerican/pop,
pct_asian = asian /pop,
pct_hispanic = hispanic/pop,
pct_twoormoreraces = twoormoreraces /pop,
pct_highed = educated/pop,
pct_unem = unemployed/(pop-notworkforce)
)
#Write CSV
install.packages("readr")
load("readr")
write.csv(demographic, file = "demographic.csv")