0

我在创建地图时为美国大陆的所有州写了一个矢量,这样就没有人需要再次手动执行此操作。这对于像底格里斯这样的包裹很重要,如果一个人只是想绘制美国大陆的地图。请看下文。有没有更有效的方法来做到这一点?

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

1 回答 1

1

我们可以使用%in%而不是==with|

library(dplyr)
allstates_sub <- allstates %>%
       filter(NAME %in% state.name)
于 2021-06-04T17:10:35.040 回答