【发布时间】:2021-03-11 12:18:49
【问题描述】:
我正在使用美国人口普查数据(也包含属性和空间数据/几何),我正在尝试将其与我在 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 值不匹配)。
【问题讨论】:
-
拥有一个包含小样本数据集的可重现示例会非常有帮助,但如果这太棘手,您能否展示使用
SDPD_Data_Census的head()的样子?如果您使用full_join()而不是left_join(),您是否会在两个有问题的列中获得任何数据?对我来说,dplyr 似乎无法在 GEOID 中找到匹配的值... -
嗨!当我在我的警察数据库上使用 {head()} 时,它显示所有正确的数据都在那里并且没有 NA 值。当我使用 {full_join()} 时,它仍然在我原来的帖子示例中显示相同的输出:(
-
我留下了一个编辑,当我使用你的两个想法时,如果这有助于更清楚地说明问题的话!再次感谢您抽出宝贵时间帮助我,我真的很感激!