busybird
busybird

Reputation: 1

R vector for mainland US states

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" )```

Upvotes: 0

Views: 369

Answers (1)

akrun
akrun

Reputation: 886928

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

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

Upvotes: 1

Related Questions