R vector for mainland US states

301 views Asked by At

I wrote a vector for all the mainland US states when creating maps, so that nobody has to do this manually again. This is important for packages like Tigris if one is only trying to map mainland US. Please see below. Is there a more efficient way to do this?

library(tigris)

allstates <- states(cb = TRUE) #this loads tigris 

allstates <- allstates %>% filter(
NAME == "Alabama" |NAME == "Arizona" |NAME == "Arkansas" | 
NAME == "California" | NAME == "Colorado" | NAME == "Connecticut" |NAME == "Delaware" |
NAME == "Florida" | NAME =="Georgia" |  NAME =="Idaho" |
NAME =="Illinois" |NAME =="Indiana" |NAME =="Iowa" |NAME =="Kansas" |
NAME =="Kentucky" |NAME =="Louisiana" |NAME =="Maine" |NAME =="Maryland" |
NAME =="Massachusetts" |NAME =="Michigan" |NAME =="Minnesota" |NAME =="Mississippi" |
NAME =="Missouri" |NAME =="Montana" |NAME =="Nebraska" |NAME =="Nevada" |
NAME =="New Hampshire" |NAME =="New Jersey" |NAME =="New Mexico" |NAME =="New York" |
NAME =="North Carolina" |NAME =="North Dakota" |NAME =="Ohio" |NAME =="Oklahoma" |
NAME =="Oregon" |NAME =="Pennsylvania" |NAME =="Rhode Island" |NAME =="South Carolina" |
NAME =="South Dakota" |NAME =="Tennessee" |NAME =="Texas" |NAME =="Utah" |NAME =="Vermont" |
NAME =="Virginia" | NAME =="Washington" | NAME =="West Virginia" | NAME =="Wisconsin" | 
NAME =="Wyoming" )```
1

There are 1 answers

2
akrun On

We could use %in% instead of == with |

library(dplyr)
allstates_sub <- allstates %>%
       filter(NAME %in% state.name)