Data can be found here (Admin 1- States and Provinces, first download button): http://www.naturalearthdata.com/downloads/10m-cultural-vectors/
I have a shapefile of the world that I'll be pulling specific countries from (it's a better fit for the data than GADM). Eventually I'll be matching the country regions (identified as "name" in the shapefile) to some created data. Some of the region names are named differently between my dataset and the shapefile, so I need to go in an manually change them. Here's my little bit of code so far:
#pull in world shapefile, remove unwanted columns world.shp <- readOGR("D:/VPI", layer = "ne_10m_admin_1_states_provinces") world.shp <- world.shp[, -(c(1:8, 10:43, 45:59))] #convert to character world.shp$admin <- as.character(world.shp$admin) world.shp$name <- as.character(world.shp$name) #manual shapefile changes for uniform mapping world.shp[world.shp$admin == "United Republic of Tanzania", "admin"] <- "Tanzania" world.shp[world.shp$name == "Zanzibar South and Central",] <- "Kusini-Unguja" world.shp[world.shp$name == "Zanzibar West", "name"] <- "Mjini Magharibi"
In this example, I was successfully able to change "United Republic of Tanzania" to simply "Tanzania." When I try to run the similar lines of code for the regional changes, I get the following error:
*tmp*, i, j, value = "Kusini-Unguja") :
missing values are not allowed in subscripted assignments of data frames
A quick search suggests that I can't rename anything in the
world.shp$name column because there are NA values present. I'm not sure how to remove these NA values while keeping the shapefile a spatial data frame. My initial thought was to run
world.shp$name <- na.omit(world.shp$name)
which resulted in the error:
*tmp*, name, value = c("Aruba", "Badghis", : replacement has 4602 rows, data has 4647
I understand that this is because I still have country names (
world.shp$admin) attached to the NA region names, so the rows aren't equal. How else can I remove these NA values so that I can reassign attribute names and keep my Spatial Polygon Data Frame?