I am trying to replicate what I did for state level and works to county level. Once I have the states spatial data frame, I convert it to a simple features object because I can treat that more like a traditional data frame. Now it can be left_joined to our USArrests data set. When I am tryin to repeat the same ptocess for county level I get a single row dataframe back.More specifically :
library(tidyverse)
library(sf)
library(leaflet)
library(tigris)
options(tigris_use_cache = TRUE)
data("USArrests", package = "datasets")
USArrests <- USArrests %>% as_tibble(rownames = "state")
states_sf <- tigris::states() %>%
as("sf") %>%
rename(state=NAME) %>%
left_join(USArrests) %>%
na.omit()
#> Joining, by = "state"
states_sf
#> Simple feature collection with 50 features and 18 fields
#> geometry type: MULTIPOLYGON
#> dimension: XY
#> bbox: xmin: -179.2311 ymin: 18.86546 xmax: 179.8597 ymax: 71.44106
#> epsg (SRID): 4269
#> proj4string: +proj=longlat +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +no_defs
#> First 10 features:
#> REGION DIVISION STATEFP STATENS GEOID STUSPS state LSAD MTFCC
#> 1 3 5 54 01779805 54 WV West Virginia 00 G4000
#> 2 3 5 12 00294478 12 FL Florida 00 G4000
#> 3 2 3 17 01779784 17 IL Illinois 00 G4000
#> 4 2 4 27 00662849 27 MN Minnesota 00 G4000
#> 5 3 5 24 01714934 24 MD Maryland 00 G4000
#> 6 1 1 44 01219835 44 RI Rhode Island 00 G4000
#> 7 4 8 16 01779783 16 ID Idaho 00 G4000
#> 8 1 1 33 01779794 33 NH New Hampshire 00 G4000
#> 9 3 5 37 01027616 37 NC North Carolina 00 G4000
#> 10 1 1 50 01779802 50 VT Vermont 00 G4000
#> FUNCSTAT ALAND AWATER INTPTLAT INTPTLON Murder
#> 1 A 62265597146 489902816 +38.6472854 -080.6183274 5.7
#> 2 A 138924199212 31386038155 +28.4574302 -082.4091478 15.4
#> 3 A 143788697679 6206693598 +40.1028754 -089.1526108 10.4
#> 4 A 206232257655 18929176411 +46.3158148 -094.1996628 2.7
#> 5 A 25147754905 6983312282 +38.9466584 -076.6744939 11.3
#> 6 A 2677898725 1323551636 +41.5974187 -071.5272723 3.4
#> 7 A 214042908012 2398669593 +44.3484222 -114.5588538 2.6
#> 8 A 23187396994 1028678842 +43.6726907 -071.5843145 2.1
#> 9 A 125921301190 13470062955 +35.5397100 -079.1308636 13.0
#> 10 A 23873467535 1031124865 +44.0604795 -072.6733274 2.2
#> Assault UrbanPop Rape geometry
#> 1 81 39 9.3 MULTIPOLYGON (((-81.74725 3...
#> 2 335 80 31.9 MULTIPOLYGON (((-82.98624 2...
#> 3 249 83 24.0 MULTIPOLYGON (((-91.18529 4...
#> 4 72 66 14.9 MULTIPOLYGON (((-96.78438 4...
#> 5 300 67 27.8 MULTIPOLYGON (((-77.45881 3...
#> 6 174 87 8.3 MULTIPOLYGON (((-71.67264 4...
#> 7 120 54 14.2 MULTIPOLYGON (((-116.8997 4...
#> 8 57 56 9.5 MULTIPOLYGON (((-72.3299 43...
#> 9 337 45 16.1 MULTIPOLYGON (((-82.41674 3...
#> 10 48 32 11.2 MULTIPOLYGON (((-73.31328 4...
When I try to do the same with counties :
library(noncensus)
data(counties)
counties_sf <- counties %>% as_tibble()
counties_sf <- tigris::counties() %>%
as("sf") %>%
rename(county_name=NAME) %>%
left_join(counties) %>%
na.omit()
counties_sf
I get a single row dataset:
The function na.omit()
will remove all rows with one missing value. That's why you have only one row at the end. Without it, your result has 3.233 rows.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.