[英]Creating counties spatial dataframe produces a single row dataframe as output unlike the states spatial dataframe
[英]Creating counties spatial dataframe produces columns with NAs where data should have been, unlike the states spatial dataframe
我正在尝试复制我在州一级所做的工作并在县级工作。 一旦有了状态空间数据框,便将其转换为简单的特征对象,因为我可以将其更像传统数据框一样对待。 现在,可以将其左连接到我们的USArrests数据集。 当我尝试在县级重复相同的过程时,我得到一个最终的数据帧,其中包含NAs
而不是县数据:
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...
当我尝试对县进行同样的操作时:
library(noncensus)
data(counties)
counties_sf <- counties %>% as_tibble()
counties_sf <- tigris::counties() %>%
as("sf") %>%
rename(county_name=NAME) %>%
left_join(counties)
counties_sf
通常,使用名称合并数据不是一个好主意。 在您的情况下,这两个数据框中的县名完全不同。 参见head(counties$county_name)
和head(counties_sf$NAME)
。
为了避免使用名称,在这种情况下可以使用FIPS代码。
library(noncensus)
library(sf)
library(tigris)
data(counties)
counties <- counties %>%
mutate(GEOID = paste0(state_fips, county_fips))
counties_sf <- tigris::counties() %>%
as("sf") %>%
left_join(counties, by = "GEOID")
head(counties_sf)
Simple feature collection with 6 features and 25 fields
geometry type: MULTIPOLYGON
dimension: XY
bbox: xmin: -123.7283 ymin: 18.11774 xmax: -65.81565 ymax: 46.38562
epsg (SRID): 4269
proj4string: +proj=longlat +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +no_defs
STATEFP COUNTYFP COUNTYNS GEOID NAME NAMELSAD LSAD CLASSFP MTFCC CSAFP CBSAFP METDIVFP FUNCSTAT ALAND AWATER INTPTLAT
1 31 039 00835841 31039 Cuming Cuming County 06 H1 G4020 <NA> <NA> <NA> A 1477895811 10447360 +41.9158651
2 53 069 01513275 53069 Wahkiakum Wahkiakum County 06 H1 G4020 <NA> <NA> <NA> A 680956787 61588406 +46.2946377
3 35 011 00933054 35011 De Baca De Baca County 06 H1 G4020 <NA> <NA> <NA> A 6016761713 29147306 +34.3592729
4 31 109 00835876 31109 Lancaster Lancaster County 06 H1 G4020 339 30700 <NA> A 2169240199 22877180 +40.7835474
5 31 129 00835886 31129 Nuckolls Nuckolls County 06 H1 G4020 <NA> <NA> <NA> A 1489645187 1718484 +40.1764918
6 72 085 01804523 72085 Las Piedras Las Piedras Municipio 13 H1 G4020 490 41980 <NA> A 87748363 32509 +18.1871483
INTPTLON county_name state state_fips county_fips fips_class CSA CBSA population geometry
1 -096.7885168 Cuming County NE 31 039 H1 <NA> <NA> 9139 MULTIPOLYGON (((-97.01952 4...
2 -123.4244583 Wahkiakum County WA 53 069 H1 <NA> <NA> 3978 MULTIPOLYGON (((-123.4364 4...
3 -104.3686961 De Baca County NM 35 011 H1 <NA> <NA> 2022 MULTIPOLYGON (((-104.5674 3...
4 -096.6886584 Lancaster County NE 31 109 H1 339 30700 285407 MULTIPOLYGON (((-96.9106 40...
5 -098.0468422 Nuckolls County NE 31 129 H1 <NA> <NA> 4500 MULTIPOLYGON (((-98.27367 4...
6 -065.8711890 Las Piedras Municipio PR 72 085 H1 <NA> <NA> NA MULTIPOLYGON (((-65.91048 1...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.