簡體   English   中英

如何合並Shapefile和數據集?

[英]How to Merge Shapefile and Dataset?

我想創建一個顯示美國各縣毒品死亡率的空間圖,但是在將毒品死亡率數據集raw_rate與shapefile usa_county_df合並時遇到了麻煩。 有人可以幫忙嗎?

我已經在兩個合並集中創建了一個關鍵變量“ County”,但是我不知道如何格式化它們以使數據可合並。 如何使County變量對應? 謝謝!

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)`

我對此。 首先,您無法期望得到完整的代碼答案,因為您沒有提供指向數據的鏈接。 下次,請提供有關數據問題的完整說明。

我只是使用您在此處提供的數據進行說明。

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)

數據鏈接: county_crude數據鏈接: usa_county

塊引用

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM