library(tidycensus)
## Warning: package 'tidycensus' was built under R version 3.4.3
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 3.4.3
## -- Attaching packages ------------------------------------------- tidyverse 1.2.1 --
## v ggplot2 2.2.1     v purrr   0.2.4
## v tibble  1.3.4     v dplyr   0.7.4
## v tidyr   0.7.2     v stringr 1.2.0
## v readr   1.1.1     v forcats 0.2.0
## Warning: package 'ggplot2' was built under R version 3.4.3
## Warning: package 'tidyr' was built under R version 3.4.3
## Warning: package 'dplyr' was built under R version 3.4.3
## -- Conflicts ---------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
#testing 
census_api_key("687a401e65631b92defbc1c5d41c7668705d2bbd")
## To install your API key for use in future sessions, run this function with `install = TRUE`.
m90 <- get_decennial(geography = "state", variables = "H043A001", year = 1990)
## [1] "Checking SF3 API for data..."
m90 %>% 
  ggplot(aes(x = value, y = reorder(NAME, value))) +
  geom_point()

nh <- get_acs(geography = "county", variables = "B19013_001", state = "NH")
head(nh)
## # A tibble: 6 x 5
##   GEOID                               NAME   variable estimate   moe
##   <chr>                              <chr>      <chr>    <dbl> <dbl>
## 1 33001      Belknap County, New Hampshire B19013_001    62159  2383
## 2 33003      Carroll County, New Hampshire B19013_001    53306  2643
## 3 33005     Cheshire County, New Hampshire B19013_001    57782  1690
## 4 33007         Coos County, New Hampshire B19013_001    42312  1911
## 5 33009      Grafton County, New Hampshire B19013_001    55762  2055
## 6 33011 Hillsborough County, New Hampshire B19013_001    71244   998
nh %>%
   mutate(NAME = gsub(" County, New Hampshire", "", NAME)) %>%
  ggplot(aes(x = estimate, y = reorder(NAME, estimate))) +
  geom_errorbarh(aes(xmin = estimate - moe, xmax = estimate + moe)) +
  geom_point(color = "red", size = 3) +
  labs(title = "Household income by county in New Hampshire",
       subtitle = "2011-2015 American Community Survey",
       y = "",
       x = "ACS estimate (bars represent margin of error)")

#testing
p <- data.frame(get_acs
                (geography = "county", variables = "B19013_001", state = "NH"))
head(p)
##   GEOID                               NAME   variable estimate  moe
## 1 33001      Belknap County, New Hampshire B19013_001    62159 2383
## 2 33003      Carroll County, New Hampshire B19013_001    53306 2643
## 3 33005     Cheshire County, New Hampshire B19013_001    57782 1690
## 4 33007         Coos County, New Hampshire B19013_001    42312 1911
## 5 33009      Grafton County, New Hampshire B19013_001    55762 2055
## 6 33011 Hillsborough County, New Hampshire B19013_001    71244  998
e <- (p[,4])
head(e)
## [1] 62159 53306 57782 42312 55762 71244
summary(e)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   42312   55830   59247   60649   65027   81198
#median age less than 41
age <- data.frame(get_acs(geography = "county", variables = "B01002_001E"
                          , state = "NH"))
head(age)
##   GEOID                               NAME   variable estimate moe
## 1 33001      Belknap County, New Hampshire B01002_001     46.1 0.3
## 2 33003      Carroll County, New Hampshire B01002_001     50.3 0.2
## 3 33005     Cheshire County, New Hampshire B01002_001     42.0 0.3
## 4 33007         Coos County, New Hampshire B01002_001     48.1 0.3
## 5 33009      Grafton County, New Hampshire B01002_001     41.9 0.3
## 6 33011 Hillsborough County, New Hampshire B01002_001     40.1 0.2
tail(age)
##    GEOID                               NAME   variable estimate moe
## 5  33009      Grafton County, New Hampshire B01002_001     41.9 0.3
## 6  33011 Hillsborough County, New Hampshire B01002_001     40.1 0.2
## 7  33013    Merrimack County, New Hampshire B01002_001     42.6 0.2
## 8  33015   Rockingham County, New Hampshire B01002_001     43.5 0.1
## 9  33017    Strafford County, New Hampshire B01002_001     37.2 0.3
## 10 33019     Sullivan County, New Hampshire B01002_001     45.1 0.3
#% of people with degree in 75th precentile 
#some college or associate
assc <- data.frame((get_acs(geography = "county", variable = "B06009_004E"
                            , state = "NH")))
head(assc)
##   GEOID                               NAME   variable estimate  moe
## 1 33001      Belknap County, New Hampshire B06009_004    13494  614
## 2 33003      Carroll County, New Hampshire B06009_004    10756  680
## 3 33005     Cheshire County, New Hampshire B06009_004    14058  680
## 4 33007         Coos County, New Hampshire B06009_004     7311  374
## 5 33009      Grafton County, New Hampshire B06009_004    15588  663
## 6 33011 Hillsborough County, New Hampshire B06009_004    78616 1604
tail(assc)
##    GEOID                               NAME   variable estimate  moe
## 5  33009      Grafton County, New Hampshire B06009_004    15588  663
## 6  33011 Hillsborough County, New Hampshire B06009_004    78616 1604
## 7  33013    Merrimack County, New Hampshire B06009_004    31019  808
## 8  33015   Rockingham County, New Hampshire B06009_004    61894 1540
## 9  33017    Strafford County, New Hampshire B06009_004    24002  921
## 10 33019     Sullivan County, New Hampshire B06009_004     8159  501
#bachelors degree
bach <- data.frame((get_acs(geography = "county", variables = "B06009_005E",
                            state = "NH")))
head(bach)
##   GEOID                               NAME   variable estimate  moe
## 1 33001      Belknap County, New Hampshire B06009_005     8412  522
## 2 33003      Carroll County, New Hampshire B06009_005     7279  512
## 3 33005     Cheshire County, New Hampshire B06009_005    10128  587
## 4 33007         Coos County, New Hampshire B06009_005     2827  220
## 5 33009      Grafton County, New Hampshire B06009_005    12404  642
## 6 33011 Hillsborough County, New Hampshire B06009_005    64234 1632
tail(bach)
##    GEOID                               NAME   variable estimate  moe
## 5  33009      Grafton County, New Hampshire B06009_005    12404  642
## 6  33011 Hillsborough County, New Hampshire B06009_005    64234 1632
## 7  33013    Merrimack County, New Hampshire B06009_005    22168  836
## 8  33015   Rockingham County, New Hampshire B06009_005    52427 1614
## 9  33017    Strafford County, New Hampshire B06009_005    17024  877
## 10 33019     Sullivan County, New Hampshire B06009_005     5145  381
#graduate professional degree
grad <- data.frame((get_acs(geography = "county", variables = "B06009_006E",
                            state = "NH")))
head(grad)
##   GEOID                               NAME   variable estimate  moe
## 1 33001      Belknap County, New Hampshire B06009_006     4606  407
## 2 33003      Carroll County, New Hampshire B06009_006     4388  398
## 3 33005     Cheshire County, New Hampshire B06009_006     6373  455
## 4 33007         Coos County, New Hampshire B06009_006     1465  177
## 5 33009      Grafton County, New Hampshire B06009_006    11021  553
## 6 33011 Hillsborough County, New Hampshire B06009_006    35770 1111
tail(grad)
##    GEOID                               NAME   variable estimate  moe
## 5  33009      Grafton County, New Hampshire B06009_006    11021  553
## 6  33011 Hillsborough County, New Hampshire B06009_006    35770 1111
## 7  33013    Merrimack County, New Hampshire B06009_006    13303  652
## 8  33015   Rockingham County, New Hampshire B06009_006    30137 1165
## 9  33017    Strafford County, New Hampshire B06009_006    10464  688
## 10 33019     Sullivan County, New Hampshire B06009_006     3171  379
#% of jobs in high wage services 
#with income $75,000 or more 
high <- data.frame((get_acs(geography = "county", variables = "B06010_011E",
                            state = "NH")))
head(high)
##   GEOID                               NAME   variable estimate  moe
## 1 33001      Belknap County, New Hampshire B06010_011     5548  398
## 2 33003      Carroll County, New Hampshire B06010_011     4255  401
## 3 33005     Cheshire County, New Hampshire B06010_011     6178  395
## 4 33007         Coos County, New Hampshire B06010_011     1344  164
## 5 33009      Grafton County, New Hampshire B06010_011     9152  537
## 6 33011 Hillsborough County, New Hampshire B06010_011    53237 1335
tail(high)
##    GEOID                               NAME   variable estimate  moe
## 5  33009      Grafton County, New Hampshire B06010_011     9152  537
## 6  33011 Hillsborough County, New Hampshire B06010_011    53237 1335
## 7  33013    Merrimack County, New Hampshire B06010_011    15983  690
## 8  33015   Rockingham County, New Hampshire B06010_011    47277 1412
## 9  33017    Strafford County, New Hampshire B06010_011    12404  648
## 10 33019     Sullivan County, New Hampshire B06010_011     3546  313
still need income volatility, gain in high wage services, gain in people 17 and under, within 1 hr of major airport, income volatility
#Farmers, ranchers, and other agricultural managers (returns NA)
#Farm employment, farm earnings, farm earnings above 5+ years
farm <- data.frame((get_acs(geography = "county", variables = "B24121_017E"
                            , state = "NH")))
head(farm)
##   GEOID                               NAME   variable estimate moe
## 1 33001      Belknap County, New Hampshire B24121_017       NA  NA
## 2 33003      Carroll County, New Hampshire B24121_017       NA  NA
## 3 33005     Cheshire County, New Hampshire B24121_017       NA  NA
## 4 33007         Coos County, New Hampshire B24121_017       NA  NA
## 5 33009      Grafton County, New Hampshire B24121_017       NA  NA
## 6 33011 Hillsborough County, New Hampshire B24121_017       NA  NA
tail(farm)
##    GEOID                               NAME   variable estimate moe
## 5  33009      Grafton County, New Hampshire B24121_017       NA  NA
## 6  33011 Hillsborough County, New Hampshire B24121_017       NA  NA
## 7  33013    Merrimack County, New Hampshire B24121_017       NA  NA
## 8  33015   Rockingham County, New Hampshire B24121_017       NA  NA
## 9  33017    Strafford County, New Hampshire B24121_017       NA  NA
## 10 33019     Sullivan County, New Hampshire B24121_017       NA  NA
#90th percentile farm earnings, missing data
farm_bls <- data.frame(read.csv("BLS.csv", header = TRUE))
head(farm_bls)
##                                                                 Area.Name
## 1                                                Bakersfield, CA(0012540)
## 2                                                 Boise City, ID(0014260)
## 3 Chicago-Naperville-Arlington Heights, IL Metropolitan Division(0016974)
## 4                             Chicago-Naperville-Elgin, IL-IN-WI(0016980)
## 5                                                   Columbus, OH(0018140)
## 6                                                     Fresno, CA(0023420)
##   X90th_percentile
## 1           130090
## 2                -
## 3            98360
## 4           103690
## 5           127780
## 6           159280
#farming fishing and forestry occupations https://data.bls.gov/oes/#/geoOcc/Multiple%20occupations%20for%20one%20geographical%20area
#complete data just needs to all be done out in excel by county and state merged together 
#https://cran.r-project.org/web/packages/blsAPI/blsAPI.pdf   BLS package 
#worked outside any metropolitan statistical area
rural <- data.frame((get_acs(geography = "county", variables = "B08016_012E"
                            , state = "NH")))
head(rural)
##   GEOID                               NAME   variable estimate moe
## 1 33001      Belknap County, New Hampshire B08016_012        0  26
## 2 33003      Carroll County, New Hampshire B08016_012        0  23
## 3 33005     Cheshire County, New Hampshire B08016_012        0  26
## 4 33007         Coos County, New Hampshire B08016_012        0  23
## 5 33009      Grafton County, New Hampshire B08016_012        0  26
## 6 33011 Hillsborough County, New Hampshire B08016_012       78  58
tail(rural)
##    GEOID                               NAME   variable estimate moe
## 5  33009      Grafton County, New Hampshire B08016_012        0  26
## 6  33011 Hillsborough County, New Hampshire B08016_012       78  58
## 7  33013    Merrimack County, New Hampshire B08016_012        0  26
## 8  33015   Rockingham County, New Hampshire B08016_012        0  26
## 9  33017    Strafford County, New Hampshire B08016_012        0  26
## 10 33019     Sullivan County, New Hampshire B08016_012        0  23
#Natural resources, construction, and maintenance occup
natural <- data.frame((get_acs(geography = "county", variables = "C24050_057E"
                            , state = "NH")))
head(natural)
##   GEOID                               NAME   variable estimate moe
## 1 33001      Belknap County, New Hampshire C24050_057     3387 311
## 2 33003      Carroll County, New Hampshire C24050_057     2728 352
## 3 33005     Cheshire County, New Hampshire C24050_057     3859 395
## 4 33007         Coos County, New Hampshire C24050_057     1684 168
## 5 33009      Grafton County, New Hampshire C24050_057     3887 312
## 6 33011 Hillsborough County, New Hampshire C24050_057    17902 950
tail(natural)
##    GEOID                               NAME   variable estimate moe
## 5  33009      Grafton County, New Hampshire C24050_057     3887 312
## 6  33011 Hillsborough County, New Hampshire C24050_057    17902 950
## 7  33013    Merrimack County, New Hampshire C24050_057     6956 558
## 8  33015   Rockingham County, New Hampshire C24050_057    15092 820
## 9  33017    Strafford County, New Hampshire C24050_057     5652 526
## 10 33019     Sullivan County, New Hampshire C24050_057     1987 241
#Geographical Mobility in the Past Year by Age
mobility <- data.frame((get_acs(geography = "county", variables = "B07001_001E"
                            , state = "NH")))
head(mobility)
##   GEOID                               NAME   variable estimate moe
## 1 33001      Belknap County, New Hampshire B07001_001    59975 146
## 2 33003      Carroll County, New Hampshire B07001_001    47103 159
## 3 33005     Cheshire County, New Hampshire B07001_001    75784 163
## 4 33007         Coos County, New Hampshire B07001_001    31580  83
## 5 33009      Grafton County, New Hampshire B07001_001    88584 126
## 6 33011 Hillsborough County, New Hampshire B07001_001   399742 378
tail(mobility)
##    GEOID                               NAME   variable estimate moe
## 5  33009      Grafton County, New Hampshire B07001_001    88584 126
## 6  33011 Hillsborough County, New Hampshire B07001_001   399742 378
## 7  33013    Merrimack County, New Hampshire B07001_001   146202 161
## 8  33015   Rockingham County, New Hampshire B07001_001   296177 352
## 9  33017    Strafford County, New Hampshire B07001_001   124160 201
## 10 33019     Sullivan County, New Hampshire B07001_001    42820 113
#a lot of different migration pattern data available by age and education
#Median income in the past 12 months --!!Total:
income <- data.frame((get_acs(geography = "county", variables = "B06011_001E"
                            , state = "NH")))
head(income)
##   GEOID                               NAME   variable estimate  moe
## 1 33001      Belknap County, New Hampshire B06011_001    30588  727
## 2 33003      Carroll County, New Hampshire B06011_001    27887 1558
## 3 33005     Cheshire County, New Hampshire B06011_001    26974  669
## 4 33007         Coos County, New Hampshire B06011_001    23182  874
## 5 33009      Grafton County, New Hampshire B06011_001    26820  825
## 6 33011 Hillsborough County, New Hampshire B06011_001    33403  557
tail(income)
##    GEOID                               NAME   variable estimate  moe
## 5  33009      Grafton County, New Hampshire B06011_001    26820  825
## 6  33011 Hillsborough County, New Hampshire B06011_001    33403  557
## 7  33013    Merrimack County, New Hampshire B06011_001    31745  576
## 8  33015   Rockingham County, New Hampshire B06011_001    37149  547
## 9  33017    Strafford County, New Hampshire B06011_001    26324  680
## 10 33019     Sullivan County, New Hampshire B06011_001    28760 1689
#Median income in the past 12 months --!!Total:
income <- data.frame((get_acs(geography = "county", variables = "B07011_001E"
                            , state = "NH")))
head(income)
##   GEOID                               NAME   variable estimate  moe
## 1 33001      Belknap County, New Hampshire B07011_001       NA   NA
## 2 33003      Carroll County, New Hampshire B07011_001    27887 1558
## 3 33005     Cheshire County, New Hampshire B07011_001       NA   NA
## 4 33007         Coos County, New Hampshire B07011_001    23182  874
## 5 33009      Grafton County, New Hampshire B07011_001    26820  825
## 6 33011 Hillsborough County, New Hampshire B07011_001       NA   NA
tail(income)
##    GEOID                               NAME   variable estimate moe
## 5  33009      Grafton County, New Hampshire B07011_001    26820 825
## 6  33011 Hillsborough County, New Hampshire B07011_001       NA  NA
## 7  33013    Merrimack County, New Hampshire B07011_001    31745 576
## 8  33015   Rockingham County, New Hampshire B07011_001    37149 547
## 9  33017    Strafford County, New Hampshire B07011_001    26324 680
## 10 33019     Sullivan County, New Hampshire B07011_001       NA  NA
#Total living in area 1 year ago:
migration <- data.frame((get_acs(geography = "county", 
                        variables = "B07410_001E", state = "NH")))
head(migration)
##   GEOID                               NAME   variable estimate  moe
## 1 33001      Belknap County, New Hampshire B07410_001    51097  623
## 2 33003      Carroll County, New Hampshire B07410_001    41278  632
## 3 33005     Cheshire County, New Hampshire B07410_001    62265  700
## 4 33007         Coos County, New Hampshire B07410_001    27340  380
## 5 33009      Grafton County, New Hampshire B07410_001    73835  956
## 6 33011 Hillsborough County, New Hampshire B07410_001   330925 1591
tail(migration)
##    GEOID                               NAME   variable estimate  moe
## 5  33009      Grafton County, New Hampshire B07410_001    73835  956
## 6  33011 Hillsborough County, New Hampshire B07410_001   330925 1591
## 7  33013    Merrimack County, New Hampshire B07410_001   122653  997
## 8  33015   Rockingham County, New Hampshire B07410_001   250238 1392
## 9  33017    Strafford County, New Hampshire B07410_001   101026 1096
## 10 33019     Sullivan County, New Hampshire B07410_001    35828  450
#Total living in area 1 year ago:!!No income
youth <- data.frame((get_acs(geography = "county", 
                        variables = "B07410_002E", state = "NH")))
head(youth)
##   GEOID                               NAME   variable estimate  moe
## 1 33001      Belknap County, New Hampshire B07410_002     5136  516
## 2 33003      Carroll County, New Hampshire B07410_002     3079  375
## 3 33005     Cheshire County, New Hampshire B07410_002     4355  360
## 4 33007         Coos County, New Hampshire B07410_002     1794  203
## 5 33009      Grafton County, New Hampshire B07410_002     6785  606
## 6 33011 Hillsborough County, New Hampshire B07410_002    32265 1259
tail(youth)
##    GEOID                               NAME   variable estimate  moe
## 5  33009      Grafton County, New Hampshire B07410_002     6785  606
## 6  33011 Hillsborough County, New Hampshire B07410_002    32265 1259
## 7  33013    Merrimack County, New Hampshire B07410_002    12269  773
## 8  33015   Rockingham County, New Hampshire B07410_002    19811  933
## 9  33017    Strafford County, New Hampshire B07410_002     7544  548
## 10 33019     Sullivan County, New Hampshire B07410_002     2908  298
#Total living in area 1 year ago:!!With income:

migrate <- data.frame((get_acs(geography = "county", 
                        variables = "B07410_003E", state = "NH")))
head(migrate)
##   GEOID                               NAME   variable estimate  moe
## 1 33001      Belknap County, New Hampshire B07410_003    45961  736
## 2 33003      Carroll County, New Hampshire B07410_003    38199  671
## 3 33005     Cheshire County, New Hampshire B07410_003    57910  678
## 4 33007         Coos County, New Hampshire B07410_003    25546  359
## 5 33009      Grafton County, New Hampshire B07410_003    67050  901
## 6 33011 Hillsborough County, New Hampshire B07410_003   298660 1816
tail(migrate)
##    GEOID                               NAME   variable estimate  moe
## 5  33009      Grafton County, New Hampshire B07410_003    67050  901
## 6  33011 Hillsborough County, New Hampshire B07410_003   298660 1816
## 7  33013    Merrimack County, New Hampshire B07410_003   110384 1174
## 8  33015   Rockingham County, New Hampshire B07410_003   230427 1605
## 9  33017    Strafford County, New Hampshire B07410_003    93482 1227
## 10 33019     Sullivan County, New Hampshire B07410_003    32920  485
#Total living in area 1 year ago:!!With income:!!$1 to $9,999 
youth_3 <- data.frame((get_acs(geography = "county", 
                        variables = "B07410_004E", state = "NH")))
head(youth_3)
##   GEOID                               NAME   variable estimate  moe
## 1 33001      Belknap County, New Hampshire B07410_004     7409  633
## 2 33003      Carroll County, New Hampshire B07410_004     7395  586
## 3 33005     Cheshire County, New Hampshire B07410_004    11064  656
## 4 33007         Coos County, New Hampshire B07410_004     5340  389
## 5 33009      Grafton County, New Hampshire B07410_004    13599  759
## 6 33011 Hillsborough County, New Hampshire B07410_004    50101 1293
tail(youth_3)
##    GEOID                               NAME   variable estimate  moe
## 5  33009      Grafton County, New Hampshire B07410_004    13599  759
## 6  33011 Hillsborough County, New Hampshire B07410_004    50101 1293
## 7  33013    Merrimack County, New Hampshire B07410_004    19500  951
## 8  33015   Rockingham County, New Hampshire B07410_004    36282 1092
## 9  33017    Strafford County, New Hampshire B07410_004    19781 1116
## 10 33019     Sullivan County, New Hampshire B07410_004     5425  444
y <- data.frame(rbind())
#Living in a principal city:
city <- data.frame((get_acs(geography = "county", 
                        variables = "B08016_002E", state = "NH")))
head(city)
##   GEOID                               NAME   variable estimate  moe
## 1 33001      Belknap County, New Hampshire B08016_002        0   26
## 2 33003      Carroll County, New Hampshire B08016_002        0   23
## 3 33005     Cheshire County, New Hampshire B08016_002        0   26
## 4 33007         Coos County, New Hampshire B08016_002        0   23
## 5 33009      Grafton County, New Hampshire B08016_002        0   26
## 6 33011 Hillsborough County, New Hampshire B08016_002   102301 1319
tail(city)
##    GEOID                               NAME   variable estimate  moe
## 5  33009      Grafton County, New Hampshire B08016_002        0   26
## 6  33011 Hillsborough County, New Hampshire B08016_002   102301 1319
## 7  33013    Merrimack County, New Hampshire B08016_002        0   26
## 8  33015   Rockingham County, New Hampshire B08016_002        0   26
## 9  33017    Strafford County, New Hampshire B08016_002        0   26
## 10 33019     Sullivan County, New Hampshire B08016_002        0   23