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:

Error in [<-.data.frame(*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:

Error in [[<-.data.frame(*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?

1 Answer 1


This should work for removing NA's for a specific column yet, retain the sp class of the object.

We will use the muese dataset from the sp library as an example

coordinates(meuse) <- ~x+y

Here we add some NA's to copper, at rows 2, 5 and 20, and look at the resulting data

meuse@data[c(2,5,20),]$copper <- NA

Using which and is.na you can return the rows in the copper column that are NA

which( is.na(meuse$copper) ) 

Expanding the use of the above statement we can remove the associated rows from the sp object.

meuse <- meuse[-which(is.na(meuse$copper)),]

If you wanted to keep the NA values you could alternately use ifelse to reassign values. Here we can assign values < 80 to 0 and not have the NA values be an issue.

meuse@data$copper <- ifelse(meuse@data$copper < 80, 0, meuse@data$copper)
  • If anybody wants an implementation of na.omit that works on sp class data, I have a function "sp.na.omit" in the spatialEco package that removes NA's values across the entire object (any columns with NA values). Using the col.name argument, it also allows for definition of a specific column. Feb 2, 2016 at 17:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.