[英]How to Merge Shapefile and Dataset?
I want to create a spatial map showing drug mortality rates by US county, but I'm having trouble merging the drug mortality dataset, crude_rate, with the shapefile, usa_county_df. 我想创建一个显示美国各县毒品死亡率的空间图,但是在将毒品死亡率数据集raw_rate与shapefile usa_county_df合并时遇到了麻烦。 Can anyone help out? 有人可以帮忙吗?
I've created a key variable, "County", in both sets to merge on but I don't know how to format them to make the data mergeable. 我已经在两个合并集中创建了一个关键变量“ County”,但是我不知道如何格式化它们以使数据可合并。 How can I make the County variables correspond? 如何使County变量对应? Thank you! 谢谢!
head(crude_rate, 5)
Notes County County.Code Deaths Population Crude.Rate
1 Autauga County, AL 1001 74 975679 7.6
2 Baldwin County, AL 1003 440 3316841 13.3
3 Barbour County, AL 1005 16 524875 Unreliable
4 Bibb County, AL 1007 50 420148 11.9
5 Blount County, AL 1009 148 1055789 14.0
head(usa_county_df, 5)
long lat order hole piece id group County
1 -97.01952 42.00410 1 FALSE 1 0 0.1 1
2 -97.01952 42.00493 2 FALSE 1 0 0.1 2
3 -97.01953 42.00750 3 FALSE 1 0 0.1 3
4 -97.01953 42.00975 4 FALSE 1 0 0.1 4
5 -97.01953 42.00978 5 FALSE 1 0 0.1 5
crude_rate$County <- as.factor(crude_rate$County)
usa_county_df$County <- as.factor(usa_county_df$County)
merge(usa_county_df, crude_rate, "County")
[1] County long lat order hole
[6] piece id group Notes County.Code
[11] Deaths Population Crude.Rate
<0 rows> (or 0-length row.names)`
My take on this. 我对此。 First, you cannot expect a full answer with code because you did not provide a link to you data. 首先,您无法期望得到完整的代码答案,因为您没有提供指向数据的链接。 Next time, please provide a full description of the problem with the data. 下次,请提供有关数据问题的完整说明。
I just used the data you provided here to illustrate. 我只是使用您在此处提供的数据进行说明。
require(tidyverse)
# Load the data
crude_rate = read.csv("county_crude.csv", header = TRUE)
usa_county = read.csv("usa_county.csv", header = TRUE)
# Create the variable "county_join" within the county_crude to "left_join" on with the usa_county data. Note that you have to have the same type of data variable between the two tables and the same values as well
crude_rate = crude_rate %>%
mutate(county_join = c(1:5))
# Join the dataframes using a left join on the county_join and County variables
df_all = usa_county %>%
left_join(crude_rate, by = c("County"="county_join")) %>%
distinct(order,hole,piece,id,group, .keep_all = TRUE)
Data link: county_crude Data link: usa_county 数据链接: county_crude数据链接: usa_county
Blockquote 块引用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.