简体   繁体   English

如何合并Shapefile和数据集?

[英]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.

相关问题 如何将shapefile与具有纬度/经度数据的数据框合并 - how to merge a shapefile with a dataframe with latitude/longitude data 如何将 shapefile 与 .csv 合并并创建图形 - How to merge a shapefile with a .csv and create a graph 如何将样本数据集从R包“spatstat”转换为shapefile - How to convert a sample dataset from the R package “spatstat” into a shapefile R 中的 Point in Polygon 方法用于合并具有超过 15&#39;000 个数据集观测值的 shapefile 区域 - Point in Polygon method in R for merge regions of a shapefile with over 15'000 observations of a dataset 如何根据R中的条件合并shapefile中包含的要素 - How to merge features contained in a shapefile based on a condition in R shapefile 和 dataset 通过坐标聚合成一个 - shapefile and dataset aggregate into one by coordinates 在 R 中的 shapefile 上绘制空间数据集时,如何 plot 坐标? - How do I plot the coordinates of a spatial dataset when plotting it over a shapefile in R? 将Shapefile,要素类或网络数据集转换为R - Shapefile, feature class or network dataset into R 为栅格堆栈中的每一层汇总跨多边形的值-如何将值与shapefile合并? - Summarizing values across polygons, for each layer in Raster Stack - How to merge values with shapefile? R 如何合并具有多个多边形的 shapefile 中的多边形特征? (可重现的代码示例) - R How do I merge polygon features in a shapefile with many polygons? (reproducible code example)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM