r - 将空间数据与非空间数据合并会产生非空间数据的 NA 值
问题描述
我正在使用美国人口普查数据(也包含属性和空间数据/几何),我正在尝试将其与我在 excel 中创建的自己的数据库(警察停止率和人口普查区域内的计数)合并并转换为 CSV文件。两个数据库共享一个唯一的列标识符“GEOID”和相同数量的观察值,但是当我使用 merge()、left_join() 甚至 inner_join() 时,我不断地从我的空间文件中获取所有数据,但变量来自我的其他数据都以 NA 的形式返回。我应该怎么办?谢谢您的帮助!
我正在使用的内容:
library(readr)
SDPD_Data_Census <- read_csv("SDPD_Data_Census.csv",
col_types = cols(GEOID = col_character(),
policestop = col_integer(), policestoprate = col_number(),
totp = col_skip()))
View(SDPD_Data_Census)
#I convert my census data into a shape file
SD.city.tracts <- st_read("SD.city.tracts.shp", stringsAsFactors = FALSE)
#My SPD_Variable_List is missing geometry data that would allow me to plot the policerate variable onto a map. To fix this, I merged my census data (that has geometry values) and my police data together
#I merge my police data with my census data using GEOID as the common factor
SD_Police_Census <- left_join(SD.city.tracts, SDPD_Data_Census)
#I use names() to check if the datasets were merged, here it shows that the policestoprate and policestop columns are now included with the census data but are showing NA values
head(SD_Police_Census, n=5)
Joining, by = "GEOID"Simple feature collection with 5 features and 34 fields
geometry type: MULTIPOLYGON
dimension: XY
bbox: xmin: -117.1949 ymin: 32.73966 xmax: -117.1554 ymax: 32.75932
epsg (SRID): NA
proj4string: +proj=longlat +ellps=GRS80 +no_defs
GEOID tpop tpopr medincome pfpov powner phsgrad pbach pdiv psingm pnhwhite nhwhite pnhasn nhasn pnhblk nhblk phisp
1 06073000100 3250 3250 138864 0.0000000 36.83077 1.969231 40.86154 7.323077 0.2153846 76.67692 2492 4.369231 142 0.0000000 0 15.876923
2 06073000201 1915 1915 90673 0.9921671 24.90862 3.342037 41.35770 12.584856 2.2454308 84.38642 1616 2.140992 41 0.5221932 10 7.049608
3 06073000202 4583 4583 66438 0.6764128 18.93956 4.494872 43.42134 12.000873 2.4874536 71.61248 3282 9.382501 430 0.8727907 40 13.855553
4 06073000300 5094 5094 69028 0.9422850 13.42756 3.945819 45.75972 13.172360 2.0416176 72.49706 3693 2.179034 111 5.1040440 260 16.195524
5 06073000400 3758 3758 75559 0.0000000 11.09633 5.268760 40.89941 11.362427 3.1665780 61.76158 2321 11.043108 415 5.0026610 188 19.425226
hisp pnonwhite nonwhite pfborn nfborn poth oth nhwhitec nonwhitec nhasnc nhblkc othc hispc tpoprc ent policestoprate policestop
1 516 23.32308 758 13.384615 435 3.076923 100 646438 853300 248715 89133 67268 448184 1499738 0.7397115 NA NA
2 135 15.61358 299 6.370757 122 5.900783 113 646438 853300 248715 89133 67268 448184 1499738 0.6069625 NA NA
3 635 28.38752 1301 15.775693 723 4.276675 196 646438 853300 248715 89133 67268 448184 1499738 0.9111694 NA NA
4 825 27.50294 1401 9.187279 468 4.024342 205 646438 853300 248715 89133 67268 448184 1499738 0.8925200 NA NA
5 730 38.23842 1437 18.121341 681 2.767429 104 646438 853300 248715 89133 67268 448184 1499738 1.1083576 NA NA
geometry
1 MULTIPOLYGON (((-117.1922 3...
2 MULTIPOLYGON (((-117.1789 3...
3 MULTIPOLYGON (((-117.1785 3...
4 MULTIPOLYGON (((-117.1686 3...
5 MULTIPOLYGON (((-117.1709 3...
#When I try to map the policestoprate variable it shows that all policestoprate data is missing
希望有人可以帮助我,我真的需要这个工作,因为它是一篇论文,我很遗憾放弃这个项目,因为有两个变量......
编辑:当我使用head(SDPD_Data_Census)
它时显示:
GEOID policestoprate policestop
<chr> <dbl> <int>
6073000100 0.0000000 0
6073000201 1.5665796 3
6073000202 0.6545931 3
6073000300 3.1409501 16
6073000400 26.3437999 99
6073000500 1.5285845 5
所以数据就在那里,并且在以原始形式保留时没有 NA 值,但是当与我的人口普查数据合并时,只有我的警察数据中的两列显示整个 NA 值。使用full_join()
也产生了相同的结果。
编辑 2:我查看了我的警察数据库,结果发现我所有的 GEOID 值在开头都缺少一个 0,这就是为什么它们无法与人口普查数据库中的 GEOID 值(其中有这些零)匹配。非常愚蠢的错误,但现在我必须在 excel 上的所有 GEOID 值中手动插入 0,希望这次它们合并。(当我full_join()
对这两个数据集进行分析时,事实证明警察数据被保留了,但它们被添加到新制作的数据集的最底部,因为它们与人口普查 GEOID 值不匹配)。
解决方案
编辑 3:我手动修复了我的警察数据库,并在我的 GEOID 前面添加了 0,以与人口普查数据库中的相匹配。之后使用full_join()
效果很好,现在我可以毫无问题地映射我的警察停止率!经验教训:尽量不要在凌晨 2 点工作,因为你可能会犯这样的愚蠢错误。