I'm not sure if I've used the correct terminology in my title, so please forgive me if that doesn't entirely make sense.
For the sake of reproducing my example, I've created a dummy example. As usual, I pulled the ESRI shapefile from GADM and am using Ethiopia (admin level 1) as an example. I'll describe my problem in terms of this Ethiopia example.
I have a shapefile of the regions of Ethiopia that has been fortified into a data frame. Each region corresponds to a number in the attribute ID_1
. I have created a vector, x
, of a string of numbers that I need to pull from the Ethiopia shapefile while maintaining all of the rest of the data. Here's my code:
setwd("D:/Mapping-R/Ethiopia")
library(rgdal)
library(ggplot2)
library(rgeos)
library(maptools)
library(plyr)
#read in shapefile
eth <- readOGR(dsn = "D:/Mapping-R/Ethiopia", layer = "ETH_adm1")
#fortify
eth@data$id <- rownames(eth@data)
eth.f <- fortify(eth, region = "id")
eth.df <- join(eth.f, eth@data, by = "id")
#create dummy vector
x <- 6:9
#try to subset data
test <- eth.df[eth.df$ID_1 == x,]
This final line of code gives me the error
Warning message: In eth.df$ID_1 == x : longer object length is not a multiple of shorter object length
How do I go about performing this subset without an error? I'm still fairly new to R and subsetting, so I feel like there's probably a pretty easy solution, but I can't seem to nail the proper search terms to find an answer. In my real code, it's necessary that the numbers I need to pull (in the example, 6:9) are a named vector, so I'm not sure how to fix the problem.