在r中的shapefile上重命名属性

时间:2014-05-10 09:02:23

标签: r shapefile rgdal

我正在尝试加入两个类似的shapefile的子集,一个来自美国,另一个来自加拿大。数据集可在此处获取:shapefiles from Geocommons

我注意到在两个文件中属性略有不同。在美国地图中,州标记为" STATE_NAME"但在加拿大地图中,属性只是" NAME"。这是一个问题,因为我无法将两个shapefile加入到一个shapefile中。有没有人为此工作?

到目前为止,这是我的代码:

require (raster)

#load in boundaries for plotting 
state <- readOGR(dsn = '/usa_state_shapefile.shp', layer = "usa_state_shapefile")
projection(state) <- CRS("+proj=longlat +ellps=WGS84 +towgs84=0,0,0,0,0,0,0 +no_defs")
# Subset US shapefile by desired states
nestates <- c("Maine", "Vermont", "Massachusetts", "New Hampshire" ,"Connecticut",
              "Rhode Island","New York","Pennsylvania", "New Jersey",
              "Maryland", "Delaware", "Virginia", "West Virginia", "North Carolina")
state.sub <- state[as.character(state@data$STATE_NAME) %in% nestates, ]
summary(state.sub)

provinces<-readOGR (dsn = '/canadian_provinces.shp', layer = "canadian_provinces")
projection(provinces) <- CRS("+proj=longlat +ellps=WGS84 +towgs84=0,0,0,0,0,0,0 +no_defs")
canprov<- c ("Quebec", "Ontario", "Newfoundland  & Labrador", "New Brunswick", "Prince Edward Island", "Nova Scotia")
provinces.sub <- provinces[as.character(provinces@data$NAME) %in% canprov,]
summary (provinces.sub)

我猜测如果我将属性重命名为&#34; NAME&#34;那么我应该能够使用某种rbindcbind函数合并两个shapefile。

2 个答案:

答案 0 :(得分:2)

我不是百分百肯定,但我认为这就是所需要的:

names(state.sub@data)[names(state.sub@data)=="STATE_NAME"] <- "NAME"

然后,您应该能够使用

加入数据集
provinces_and_states <- rbind(state.sub, provinces.sub)

或者,您可以从Natural Earth获取数据。

答案 1 :(得分:0)

如果您只有一个名称,这也有效:

names(shpdata@data) <- "newname"