0

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" )```
  • Try putting the states into a vector of their own (e.g, `the.states <- c("Alabama", "Arizona", ...)`). Then when you call `filter()` just specify `NAME %in% the.states`. – daileyco Jun 14 '21 at 01:57

1 Answers1

1

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

library(dplyr)
allstates_sub <- allstates %>%
       filter(NAME %in% state.name)
akrun
  • 874,273
  • 37
  • 540
  • 662