library(sf)
## Linking to GEOS 3.8.1, GDAL 3.1.1, PROJ 6.3.1
library(sp)
library(spData)
library(raster)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:raster':
## 
##     intersect, select, union
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(stringr)
library(tidyr)
## 
## Attaching package: 'tidyr'
## The following object is masked from 'package:raster':
## 
##     extract
library(ggplot2)
library(classInt)
library(RColorBrewer)

seminar <- st_read("SeminarR.gdb")
## Multiple layers are present in data source /Users/devinhainje/Desktop/R/SeminarR.gdb, reading layer `USA_counties'.
## Use `st_layers' to list all layer names and their type in a data source.
## Set the `layer' argument in `st_read' to read a particular layer.
## Warning in evalq((function (..., call. = TRUE, immediate. = FALSE, noBreaks. =
## FALSE, : automatically selected the first layer in a data source containing more
## than one.
## Reading layer `USA_counties' from data source `/Users/devinhainje/Desktop/R/SeminarR.gdb' using driver `OpenFileGDB'
## Simple feature collection with 3140 features and 53 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: -6293474 ymin: 311822.4 xmax: 2256319 ymax: 6198811
## projected CRS:  NAD83 / Conus Albers
layersR <- st_layers("SeminarR.gdb")
layersR
## Driver: OpenFileGDB 
## Available layers:
##     layer_name     geometry_type features fields
## 1 USA_counties     Multi Polygon     3140     53
## 2    USA_roads Multi Line String      679      8
Counties <- st_read("SeminarR.gdb", layer = "USA_counties")
## Reading layer `USA_counties' from data source `/Users/devinhainje/Desktop/R/SeminarR.gdb' using driver `OpenFileGDB'
## Simple feature collection with 3140 features and 53 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: -6293474 ymin: 311822.4 xmax: 2256319 ymax: 6198811
## projected CRS:  NAD83 / Conus Albers
head(Counties)
## Simple feature collection with 6 features and 53 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: -1826165 ymin: 2819070 xmax: 117332.6 ymax: 3127678
## projected CRS:  NAD83 / Conus Albers
##                NAME STATE_NAME STATE_FIPS CNTY_FIPS  FIPS POP1990 POP1999
## 1 Lake of the Woods  Minnesota         27       077 27077    4076    4597
## 2             Ferry Washington         53       019 53019    6295    7150
## 3           Stevens Washington         53       065 53065   30948   39965
## 4          Okanogan Washington         53       047 53047   33350   38596
## 5      Pend Oreille Washington         53       051 53051    8915   11788
## 6          Boundary      Idaho         16       021 16021    8332    9840
##   POP90_SQMI HOUSEHOLDS MALES FEMALES WHITE BLACK AMERI_ES ASIAN_PI OTHER
## 1          2       1576  2037    2039  4042     1       19       10     4
## 2          3       2247  3280    3015  5084    20     1131       24    36
## 3         12      11241 15454   15494 28747    65     1807      179   150
## 4          6      12654 16828   16522 27615    52     3597      166  1920
## 5          6       3395  4426    4489  8640    12      206       25    32
## 6          7       2857  4252    4080  7950     3      150       26   203
##   HISPANIC AGE_UNDER5 AGE_5_17 AGE_18_29 AGE_30_49 AGE_50_64 AGE_65_UP
## 1       25        337      791       549      1122       584       693
## 2       85        486     1499       887      1929       827       667
## 3      483       2271     7486      3586      9605      4145      3855
## 4     2779       2536     7051      4492      9749      4890      4632
## 5      120        660     1963       996      2670      1384      1242
## 6      310        635     2065      1138      2351      1121      1022
##   NEVERMARRY MARRIED SEPARATED WIDOWED DIVORCED HSEHLD_1_M HSEHLD_1_F MARHH_CHD
## 1        538    2102        24     248      200        185        184       465
## 2       1118    2741        82     251      498        302        195       652
## 3       4037   14795       384    1486     2096       1146       1210      3618
## 4       5042   15320       561    1889     2406       1410       1633      3355
## 5       1124    4349       114     507      641        373        402       961
## 6       1247    3935        89     391      480        300        299       938
##   MARHH_NO_C MHH_CHILD FHH_CHILD HSE_UNITS VACANT OWNER_OCC RENTER_OCC
## 1        565        26        57      3050   1474      1332        244
## 2        660        90       169      3239    992      1568        679
## 3       3591       292       731     14601   3360      8566       2675
## 4       3946       401       933     16629   3975      8439       4215
## 5       1157        75       229      5404   2009      2500        895
## 6        928        55       164      3242    385      2237        620
##   MEDIAN_VAL MEDIANRENT UNITS_1DET UNITS_1ATT UNITS2 UNITS3_9 UNITS10_49
## 1      40900        185       1927         14     24       29         80
## 2      50100        197       2128         10     40       51         44
## 3      55900        231      10388        109    133      324        264
## 4      50300        222      11281        147    346      816        286
## 5      49500        237       3915         29     41       97         89
## 6      49500        217       2393          7     31      107         34
##   UNITS50_UP MOBILEHOME NO_FARMS87 AVG_SIZE87 CROP_ACR87 AVG_SALE87
## 1          0        937        222        536      83787      27958
## 2          0        936        218       3489      29482      22155
## 3          0       3264       1073        490     131700      18138
## 4          0       3431       1476        907     144053      71970
## 5          0       1185        227        276      22923      10367
## 6          0        643        297        267      51806      29463
##   Shape_Length  Shape_Area                          Shape
## 1     370200.2  4620576797 MULTIPOLYGON (((49031.84 28...
## 2     361954.6  5905616645 MULTIPOLYGON (((-1704274 29...
## 3     453727.4  6552443738 MULTIPOLYGON (((-1598429 29...
## 4     582790.3 13742574263 MULTIPOLYGON (((-1713358 29...
## 5     298909.7  3742506837 MULTIPOLYGON (((-1574882 30...
## 6     254544.8  3313277935 MULTIPOLYGON (((-1549203 30...
roads <- st_read("SeminarR.gdb", layer = "USA_roads")
## Reading layer `USA_roads' from data source `/Users/devinhainje/Desktop/R/SeminarR.gdb' using driver `OpenFileGDB'
## Simple feature collection with 679 features and 8 fields
## geometry type:  MULTILINESTRING
## dimension:      XY
## bbox:           xmin: -6146140 ymin: 316215.5 xmax: 2242259 ymax: 5729382
## projected CRS:  NAD83 / Conus Albers
cities <- st_read("cities1.csv")
## Reading layer `cities1' from data source `/Users/devinhainje/Desktop/R/cities1.csv' using driver `CSV'
## Warning: no simple feature geometries present: returning a data.frame or tbl_df
nrow(cities)
## [1] 23435
crs(roads)
## CRS arguments:
##  +proj=aea +lat_0=23 +lon_0=-96 +lat_1=29.5 +lat_2=45.5 +x_0=0 +y_0=0
## +datum=NAD83 +units=m +no_defs
crs(cities)
## [1] NA
class(cities)
## [1] "data.frame"
cities2 <- st_as_sf(cities, coords = c("long", "lat"), crs = 4269)
crs(cities2)
## CRS arguments: +proj=longlat +datum=NAD83 +no_defs
cities3 <- st_transform(cities2, st_crs(roads))
crs(cities3)
## CRS arguments:
##  +proj=aea +lat_0=23 +lon_0=-96 +lat_1=29.5 +lat_2=45.5 +x_0=0 +y_0=0
## +datum=NAD83 +units=m +no_defs
roads
## Simple feature collection with 679 features and 8 fields
## geometry type:  MULTILINESTRING
## dimension:      XY
## bbox:           xmin: -6146140 ymin: 316215.5 xmax: 2242259 ymax: 5729382
## projected CRS:  NAD83 / Conus Albers
## First 10 features:
##     LENGTH               TYPE ADMN_CLASS TOLL_RD RTE_NUM1 RTE_NUM2
## 1  140.931 Multi-Lane Divided Interstate       N       82         
## 2  186.060 Multi-Lane Divided Interstate       N       84         
## 3    8.642 Multi-Lane Divided Interstate       N       94         
## 4    9.439 Multi-Lane Divided Interstate       N       94         
## 5    8.208 Multi-Lane Divided Interstate       N       35         
## 6   22.106 Multi-Lane Divided Interstate       N      494         
## 7   18.154 Multi-Lane Divided Interstate       N       35         
## 8    8.297 Multi-Lane Divided Interstate       N       35         
## 9  156.827 Multi-Lane Divided Interstate       N       90         
## 10  74.106 Multi-Lane Divided Interstate       N       35         
##                        ROUTE Shape_Length                          Shape
## 1             Interstate  82    226804.48 MULTILINESTRING ((-1785974 ...
## 2  Interstate (OR/ID/UT)  84    299427.61 MULTILINESTRING ((-1785974 ...
## 3             Interstate  94     13908.32 MULTILINESTRING ((213049.5 ...
## 4             Interstate  94     15190.89 MULTILINESTRING ((215235.7 ...
## 5             Interstate  35     13208.87 MULTILINESTRING ((212781.6 ...
## 6             Interstate 494     35576.04 MULTILINESTRING ((201766 24...
## 7             Interstate  35     29216.80 MULTILINESTRING ((228943.8 ...
## 8             Interstate  35     13352.68 MULTILINESTRING ((212781.6 ...
## 9             Interstate  90    252386.65 MULTILINESTRING ((-845058.6...
## 10            Interstate  35    119263.95 MULTILINESTRING ((214968.3 ...
cities3
## Simple feature collection with 23435 features and 7 fields
## geometry type:  POINT
## dimension:      XY
## bbox:           xmin: -6238723 ymin: 279478.7 xmax: 2250123 ymax: 6181247
## projected CRS:  NAD83 / Conus Albers
## First 10 features:
##    FID       AREANAME CLASS ST STFIPS HOUSEUNITS POPULATION
## 1    0      Abbeville  city AL      1       1320       3173
## 2    1     Adamsville  city AL      1       1554       4161
## 3    2        Addison  town AL      1        286        626
## 4    3          Akron  town AL      1        220        468
## 5    4      Alabaster  city AL      1       5144      14732
## 6    5    Albertville  city AL      1       6238      14507
## 7    6 Alexander City  city AL      1       6170      14917
## 8    7     Aliceville  city AL      1       1293       3009
## 9    8        Allgood  town AL      1        188        464
## 10   9        Altoona  town AL      1        405        960
##                    geometry
## 1   POINT (1013892 1001068)
## 2  POINT (832138.6 1209416)
## 3  POINT (805695.3 1274759)
## 4  POINT (767443.9 1122862)
## 5  POINT (848347.7 1169580)
## 6  POINT (893133.4 1290370)
## 7    POINT (933935 1144671)
## 8  POINT (726298.6 1147252)
## 9  POINT (869036.9 1247921)
## 10 POINT (886294.4 1263702)
ggplot() + geom_sf(data = roads) + geom_sf(data = cities3)

#lots of cites lol 

road_buf <- st_buffer(roads, 20000)
plot(road_buf)

plot(roads)

road_buf
## Simple feature collection with 679 features and 8 fields
## geometry type:  POLYGON
## dimension:      XY
## bbox:           xmin: -6166138 ymin: 296217 xmax: 2262258 ymax: 5749381
## projected CRS:  NAD83 / Conus Albers
## First 10 features:
##     LENGTH               TYPE ADMN_CLASS TOLL_RD RTE_NUM1 RTE_NUM2
## 1  140.931 Multi-Lane Divided Interstate       N       82         
## 2  186.060 Multi-Lane Divided Interstate       N       84         
## 3    8.642 Multi-Lane Divided Interstate       N       94         
## 4    9.439 Multi-Lane Divided Interstate       N       94         
## 5    8.208 Multi-Lane Divided Interstate       N       35         
## 6   22.106 Multi-Lane Divided Interstate       N      494         
## 7   18.154 Multi-Lane Divided Interstate       N       35         
## 8    8.297 Multi-Lane Divided Interstate       N       35         
## 9  156.827 Multi-Lane Divided Interstate       N       90         
## 10  74.106 Multi-Lane Divided Interstate       N       35         
##                        ROUTE Shape_Length                          Shape
## 1             Interstate  82    226804.48 POLYGON ((-1881179 2860185,...
## 2  Interstate (OR/ID/UT)  84    299427.61 POLYGON ((-2073004 2786723,...
## 3             Interstate  94     13908.32 POLYGON ((232986.1 2454433,...
## 4             Interstate  94     15190.89 POLYGON ((211826.5 2425524,...
## 5             Interstate  35     13208.87 POLYGON ((192631.4 2433567,...
## 6             Interstate 494     35576.04 POLYGON ((220474.6 2451199,...
## 7             Interstate  35     29216.80 POLYGON ((244613.6 2431884,...
## 8             Interstate  35     13352.68 POLYGON ((194361.3 2424967,...
## 9             Interstate  90    252386.65 POLYGON ((-931544.5 2571693...
## 10            Interstate  35    119263.95 POLYGON ((195456.3 2313764,...
roads
## Simple feature collection with 679 features and 8 fields
## geometry type:  MULTILINESTRING
## dimension:      XY
## bbox:           xmin: -6146140 ymin: 316215.5 xmax: 2242259 ymax: 5729382
## projected CRS:  NAD83 / Conus Albers
## First 10 features:
##     LENGTH               TYPE ADMN_CLASS TOLL_RD RTE_NUM1 RTE_NUM2
## 1  140.931 Multi-Lane Divided Interstate       N       82         
## 2  186.060 Multi-Lane Divided Interstate       N       84         
## 3    8.642 Multi-Lane Divided Interstate       N       94         
## 4    9.439 Multi-Lane Divided Interstate       N       94         
## 5    8.208 Multi-Lane Divided Interstate       N       35         
## 6   22.106 Multi-Lane Divided Interstate       N      494         
## 7   18.154 Multi-Lane Divided Interstate       N       35         
## 8    8.297 Multi-Lane Divided Interstate       N       35         
## 9  156.827 Multi-Lane Divided Interstate       N       90         
## 10  74.106 Multi-Lane Divided Interstate       N       35         
##                        ROUTE Shape_Length                          Shape
## 1             Interstate  82    226804.48 MULTILINESTRING ((-1785974 ...
## 2  Interstate (OR/ID/UT)  84    299427.61 MULTILINESTRING ((-1785974 ...
## 3             Interstate  94     13908.32 MULTILINESTRING ((213049.5 ...
## 4             Interstate  94     15190.89 MULTILINESTRING ((215235.7 ...
## 5             Interstate  35     13208.87 MULTILINESTRING ((212781.6 ...
## 6             Interstate 494     35576.04 MULTILINESTRING ((201766 24...
## 7             Interstate  35     29216.80 MULTILINESTRING ((228943.8 ...
## 8             Interstate  35     13352.68 MULTILINESTRING ((212781.6 ...
## 9             Interstate  90    252386.65 MULTILINESTRING ((-845058.6...
## 10            Interstate  35    119263.95 MULTILINESTRING ((214968.3 ...
cities_int <- st_intersection(road_buf, cities3)
## Warning: attribute variables are assumed to be spatially constant throughout all
## geometries
cities_int
## Simple feature collection with 33574 features and 15 fields
## geometry type:  POINT
## dimension:      XY
## bbox:           xmin: -6147074 ymin: 320719.5 xmax: 2250123 ymax: 5728886
## projected CRS:  NAD83 / Conus Albers
## First 10 features:
##        LENGTH               TYPE ADMN_CLASS TOLL_RD RTE_NUM1 RTE_NUM2
## 294    19.394 Multi-Lane Divided Interstate       N       20       59
## 545     4.023 Multi-Lane Divided Interstate       N       20       59
## 546   185.079 Multi-Lane Divided Interstate       N       65         
## 552     9.462 Multi-Lane Divided Interstate       N       65         
## 307   130.709 Multi-Lane Divided Interstate       N       20       59
## 293    13.861 Multi-Lane Divided Interstate       N      459         
## 567    80.676 Multi-Lane Divided Interstate       N       65         
## 544   128.992 Multi-Lane Divided Interstate       N       59         
## 544.1 128.992 Multi-Lane Divided Interstate       N       59         
## 547   139.752 Multi-Lane Divided Interstate       N       20         
##                ROUTE Shape_Length FID   AREANAME CLASS ST STFIPS HOUSEUNITS
## 294   Interstate  20    31212.462   1 Adamsville  city AL      1       1554
## 545   Interstate  20     6475.059   1 Adamsville  city AL      1       1554
## 546   Interstate  65   297864.061   1 Adamsville  city AL      1       1554
## 552   Interstate  65    15228.694   1 Adamsville  city AL      1       1554
## 307   Interstate  20   210355.823   3      Akron  town AL      1        220
## 293   Interstate 459    22306.822   4  Alabaster  city AL      1       5144
## 567   Interstate  65   129838.057   4  Alabaster  city AL      1       5144
## 544   Interstate  59   207595.534   8    Allgood  town AL      1        188
## 544.1 Interstate  59   207595.534   9    Altoona  town AL      1        405
## 547   Interstate  20   224905.166  12   Anniston  city AL      1      12100
##       POPULATION                    Shape
## 294         4161 POINT (832138.6 1209416)
## 545         4161 POINT (832138.6 1209416)
## 546         4161 POINT (832138.6 1209416)
## 552         4161 POINT (832138.6 1209416)
## 307          468 POINT (767443.9 1122862)
## 293        14732 POINT (848347.7 1169580)
## 567        14732 POINT (848347.7 1169580)
## 544          464 POINT (869036.9 1247921)
## 544.1        960 POINT (886294.4 1263702)
## 547        26623 POINT (936246.4 1227518)
city_2 <- st_intersects(road_buf, cities3)
nrow(city_2)
## [1] 679
city_2
## Sparse geometry binary predicate list of length 679, where the predicate was `intersects'
## first 10 elements:
##  1: 17030, 17070, 17080, 17199, 17219, 21582, 21597, 21658, 21674, 21682, ...
##  2: 16966, 16970, 16982, 16984, 16996, 16998, 16999, 17001, 17003, 17004, ...
##  3: 9737, 9741, 9792, 9795, 9812, 9813, 9848, 9857, 9879, 9884, ...
##  4: 9741, 9787, 9795, 9812, 9813, 9857, 9879, 9895, 9916, 9934, ...
##  5: 9739, 9741, 9795, 9812, 9813, 9828, 9879, 9895, 9908, 9934, ...
##  6: 9737, 9739, 9792, 9795, 9812, 9813, 9828, 9842, 9848, 9850, ...
##  7: 9739, 9741, 9787, 9795, 9828, 9873, 9879, 9888, 9916, 9934, ...
##  8: 9739, 9795, 9828, 9873, 9908, 9934, 9942, 9945, 9952, 9973, ...
##  9: 11878, 11900, 11933, 11964, 11965, 22895, 22906, 22966, 22975
##  10: 9726, 9739, 9794, 9795, 9828, 9862, 9873, 9882, 9931, 9934, ...
class(city_2)
## [1] "sgbp" "list"
nrow(cities_int)
## [1] 33574
cities_int
## Simple feature collection with 33574 features and 15 fields
## geometry type:  POINT
## dimension:      XY
## bbox:           xmin: -6147074 ymin: 320719.5 xmax: 2250123 ymax: 5728886
## projected CRS:  NAD83 / Conus Albers
## First 10 features:
##        LENGTH               TYPE ADMN_CLASS TOLL_RD RTE_NUM1 RTE_NUM2
## 294    19.394 Multi-Lane Divided Interstate       N       20       59
## 545     4.023 Multi-Lane Divided Interstate       N       20       59
## 546   185.079 Multi-Lane Divided Interstate       N       65         
## 552     9.462 Multi-Lane Divided Interstate       N       65         
## 307   130.709 Multi-Lane Divided Interstate       N       20       59
## 293    13.861 Multi-Lane Divided Interstate       N      459         
## 567    80.676 Multi-Lane Divided Interstate       N       65         
## 544   128.992 Multi-Lane Divided Interstate       N       59         
## 544.1 128.992 Multi-Lane Divided Interstate       N       59         
## 547   139.752 Multi-Lane Divided Interstate       N       20         
##                ROUTE Shape_Length FID   AREANAME CLASS ST STFIPS HOUSEUNITS
## 294   Interstate  20    31212.462   1 Adamsville  city AL      1       1554
## 545   Interstate  20     6475.059   1 Adamsville  city AL      1       1554
## 546   Interstate  65   297864.061   1 Adamsville  city AL      1       1554
## 552   Interstate  65    15228.694   1 Adamsville  city AL      1       1554
## 307   Interstate  20   210355.823   3      Akron  town AL      1        220
## 293   Interstate 459    22306.822   4  Alabaster  city AL      1       5144
## 567   Interstate  65   129838.057   4  Alabaster  city AL      1       5144
## 544   Interstate  59   207595.534   8    Allgood  town AL      1        188
## 544.1 Interstate  59   207595.534   9    Altoona  town AL      1        405
## 547   Interstate  20   224905.166  12   Anniston  city AL      1      12100
##       POPULATION                    Shape
## 294         4161 POINT (832138.6 1209416)
## 545         4161 POINT (832138.6 1209416)
## 546         4161 POINT (832138.6 1209416)
## 552         4161 POINT (832138.6 1209416)
## 307          468 POINT (767443.9 1122862)
## 293        14732 POINT (848347.7 1169580)
## 567        14732 POINT (848347.7 1169580)
## 544          464 POINT (869036.9 1247921)
## 544.1        960 POINT (886294.4 1263702)
## 547        26623 POINT (936246.4 1227518)
#33574
nrow(cities3)
## [1] 23435
#23435

class(cities_int)
## [1] "sf"         "data.frame"
new_cities <- na.omit(cities_int)
new_cities
## Simple feature collection with 33574 features and 15 fields
## geometry type:  POINT
## dimension:      XY
## bbox:           xmin: -6147074 ymin: 320719.5 xmax: 2250123 ymax: 5728886
## projected CRS:  NAD83 / Conus Albers
## First 10 features:
##        LENGTH               TYPE ADMN_CLASS TOLL_RD RTE_NUM1 RTE_NUM2
## 294    19.394 Multi-Lane Divided Interstate       N       20       59
## 545     4.023 Multi-Lane Divided Interstate       N       20       59
## 546   185.079 Multi-Lane Divided Interstate       N       65         
## 552     9.462 Multi-Lane Divided Interstate       N       65         
## 307   130.709 Multi-Lane Divided Interstate       N       20       59
## 293    13.861 Multi-Lane Divided Interstate       N      459         
## 567    80.676 Multi-Lane Divided Interstate       N       65         
## 544   128.992 Multi-Lane Divided Interstate       N       59         
## 544.1 128.992 Multi-Lane Divided Interstate       N       59         
## 547   139.752 Multi-Lane Divided Interstate       N       20         
##                ROUTE Shape_Length FID   AREANAME CLASS ST STFIPS HOUSEUNITS
## 294   Interstate  20    31212.462   1 Adamsville  city AL      1       1554
## 545   Interstate  20     6475.059   1 Adamsville  city AL      1       1554
## 546   Interstate  65   297864.061   1 Adamsville  city AL      1       1554
## 552   Interstate  65    15228.694   1 Adamsville  city AL      1       1554
## 307   Interstate  20   210355.823   3      Akron  town AL      1        220
## 293   Interstate 459    22306.822   4  Alabaster  city AL      1       5144
## 567   Interstate  65   129838.057   4  Alabaster  city AL      1       5144
## 544   Interstate  59   207595.534   8    Allgood  town AL      1        188
## 544.1 Interstate  59   207595.534   9    Altoona  town AL      1        405
## 547   Interstate  20   224905.166  12   Anniston  city AL      1      12100
##       POPULATION                    Shape
## 294         4161 POINT (832138.6 1209416)
## 545         4161 POINT (832138.6 1209416)
## 546         4161 POINT (832138.6 1209416)
## 552         4161 POINT (832138.6 1209416)
## 307          468 POINT (767443.9 1122862)
## 293        14732 POINT (848347.7 1169580)
## 567        14732 POINT (848347.7 1169580)
## 544          464 POINT (869036.9 1247921)
## 544.1        960 POINT (886294.4 1263702)
## 547        26623 POINT (936246.4 1227518)
library_join <- cities_int %>% select(ST, POPULATION) %>%
  st_join(road_buf) %>% group_by(ST) %>% summarise(mean = mean(as.numeric(POPULATION)))
## `summarise()` ungrouping output (override with `.groups` argument)
library_join
## Simple feature collection with 51 features and 2 fields
## geometry type:  GEOMETRY
## dimension:      XY
## bbox:           xmin: -6147074 ymin: 320719.5 xmax: 2250123 ymax: 5728886
## projected CRS:  NAD83 / Conus Albers
## # A tibble: 51 x 3
##    ST       mean                                                           Shape
##    <chr>   <dbl>                                                  <GEOMETRY [m]>
##  1 AK      5049. MULTIPOINT ((-3243604 4998339), (-3222090 5043018), (-3219357 …
##  2 AL     20281. MULTIPOINT ((712433.5 1069506), (715340.7 1101958), (719217.4 …
##  3 AR     12615. MULTIPOINT ((145482.9 1369212), (148202.4 1378079), (153194.7 …
##  4 AZ     94708. MULTIPOINT ((-1735191 1229715), (-1725270 1236880), (-1706563 …
##  5 CA     71213. MULTIPOINT ((-2290566 1930753), (-2289130 1933093), (-2287655 …
##  6 CO     38879. MULTIPOINT ((-1086478 1865316), (-1081047 1856503), (-1072702 …
##  7 CT     24749. MULTIPOINT ((1851766 2264549), (1852069 2286089), (1852849 225…
##  8 DC    606900                                          POINT (1620172 1926646)
##  9 DE      9636. MULTIPOINT ((1707720 2033320), (1710851 2040225), (1711399 203…
## 10 FL     20922. MULTIPOINT ((827611.7 887086.3), (831276.2 858756), (832453.9 …
## # … with 41 more rows
ggplot() + geom_sf(data = road_buf)

ggplot() + geom_sf(data = roads) + geom_sf(data = cities_int)

##############################################

elevation <- raster("Terrain1.tif")
plot(elevation)

cities3$elev = raster::extract(elevation, cities3)
head(cities3$elev)
## [1] 123.1404 175.6370 235.7433  41.9953 157.3699 320.6448
head(cities3)
## Simple feature collection with 6 features and 8 fields
## geometry type:  POINT
## dimension:      XY
## bbox:           xmin: 767443.9 ymin: 1001068 xmax: 1013892 ymax: 1290370
## projected CRS:  NAD83 / Conus Albers
##   FID    AREANAME CLASS ST STFIPS HOUSEUNITS POPULATION
## 1   0   Abbeville  city AL      1       1320       3173
## 2   1  Adamsville  city AL      1       1554       4161
## 3   2     Addison  town AL      1        286        626
## 4   3       Akron  town AL      1        220        468
## 5   4   Alabaster  city AL      1       5144      14732
## 6   5 Albertville  city AL      1       6238      14507
##                   geometry     elev
## 1  POINT (1013892 1001068) 123.1404
## 2 POINT (832138.6 1209416) 175.6370
## 3 POINT (805695.3 1274759) 235.7433
## 4 POINT (767443.9 1122862)  41.9953
## 5 POINT (848347.7 1169580) 157.3699
## 6 POINT (893133.4 1290370) 320.6448
cities5 <- na.omit(cities3)

cities4 <- cities5 %>% group_by(ST) %>% summarise(mean= mean(elev), stdev = sd(elev))
## `summarise()` ungrouping output (override with `.groups` argument)
cities4
## Simple feature collection with 49 features and 3 fields
## geometry type:  GEOMETRY
## dimension:      XY
## bbox:           xmin: -2345385 ymin: 279478.7 xmax: 2250123 ymax: 3164337
## projected CRS:  NAD83 / Conus Albers
## # A tibble: 49 x 4
##    ST      mean stdev                                                   geometry
##    <chr>  <dbl> <dbl>                                             <GEOMETRY [m]>
##  1 AL     156.   92.4 MULTIPOINT ((712433.5 1069506), (712934.2 1147641), (7142…
##  2 AR     137.  104.  MULTIPOINT ((130060.1 1459923), (135039.4 1469705), (1368…
##  3 AZ    1025.  616.  MULTIPOINT ((-1743743 1219525), (-1735191 1229715), (-172…
##  4 CA     248.  370.  MULTIPOINT ((-2345385 2109126), (-2337473 2302033), (-233…
##  5 CO    1852.  506.  MULTIPOINT ((-1122675 1712945), (-1115604 1649092), (-110…
##  6 CT      80.9  75.6 MULTIPOINT ((1846420 2335730), (1851766 2264549), (185206…
##  7 DC      17.3  NA                                      POINT (1620172 1926646)
##  8 DE      15.1  19.6 MULTIPOINT ((1707720 2033320), (1710851 2040225), (171139…
##  9 FL      13.7  16.3 MULTIPOINT ((827611.7 887086.3), (831081.8 916217.6), (83…
## 10 GA     185.  127.  MULTIPOINT ((949183.4 1365255), (956970 1322049), (961544…
## # … with 39 more rows
###############################################

usa <- Counties %>% group_by(STATE_NAME, AGE_5_17, AGE_65_UP) %>% summarise(rate = n(), ages = AGE_5_17 - AGE_65_UP)
## `summarise()` regrouping output by 'STATE_NAME', 'AGE_5_17' (override with `.groups` argument)
head(usa)
## Simple feature collection with 6 features and 5 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: 723703.4 ymin: 977214.5 xmax: 992039.7 ymax: 1264708
## projected CRS:  NAD83 / Conus Albers
## # A tibble: 6 x 6
## # Groups:   STATE_NAME, AGE_5_17 [6]
##   STATE_NAME AGE_5_17 AGE_65_UP  rate  ages                                Shape
##   <chr>         <int>     <int> <int> <int>                   <MULTIPOLYGON [m]>
## 1 Alabama        2092      1602     1   490 (((878438.1 1150754, 878833.2 11583…
## 2 Alabama        2433      1773     1   660 (((991792.4 1064421, 991919.7 10633…
## 3 Alabama        2458      1705     1   753 (((980155.2 1231475, 984990.1 12135…
## 4 Alabama        2530      2232     1   298 (((925603.9 1162438, 925326.3 11642…
## 5 Alabama        2555      1590     1   965 (((768565.7 1138696, 768945.6 11361…
## 6 Alabama        2748      2496     1   252 (((896998.5 1033689, 901207 1034690…
class(usa)
## [1] "sf"         "grouped_df" "tbl_df"     "tbl"        "data.frame"
plot(usa$ages)

plot(usa["ages"], axes=TRUE, main= "Difference in 5-17 and 65-up ages in the United States", 
     nbreaks=5,breaks="quantile")