您需要将状态名称折叠成一个字符串,然后使用
str_extract
从中提取名称。
library(dplyr)
library(stringr)
df %>%
mutate(state = str_extract(quote,str_c(state.name, collapse = "|")))
# num quote state
# <dbl> <chr> <chr>
#1 11 In Ohio, there are plenty of hobos Ohio
#2 12 Georgia, where the peaches are peachy Georgia
#3 13 Oregon, no, we did not die of dysentery Oregon
哪里
str_c
生成此字符串。
str_c(state.name, collapse = "|")
[1] "Alabama|Alaska|Arizona|Arkansas|California|Colorado|Connecticut|Delaware|Florida|Georgia|Hawaii|Idaho|Illinois|Indiana|Iowa|Kansas|Kentucky|Louisiana|Maine|Maryland|Massachusetts|Michigan|Minnesota|Mississippi|Missouri|Montana|Nebraska|Nevada|New Hampshire|New Jersey|New Mexico|New York|North Carolina|North Dakota|Ohio|Oklahoma|Oregon|Pennsylvania|Rhode Island|South Carolina|South Dakota|Tennessee|Texas|Utah|Vermont|Virginia|Washington|West Virginia|Wisconsin|Wyoming"