Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何识别字符串中提到的所有国家名称并进行相应拆分?_R_Maps_Stringr - Fatal编程技术网

R 如何识别字符串中提到的所有国家名称并进行相应拆分?

R 如何识别字符串中提到的所有国家名称并进行相应拆分?,r,maps,stringr,R,Maps,Stringr,我有一个包含国家和其他地区名称的字符串。我只对国家名称感兴趣,最好添加几个列,每个列都包含字符串中列出的国家名称。以下是数据帧lis设置方式的示例代码: df <- data.frame(id = c(1,2,3), country = c("Cote d'Ivoire Africa Developing Economies West Africa", "South

我有一个包含国家和其他地区名称的字符串。我只对国家名称感兴趣,最好添加几个列,每个列都包含字符串中列出的国家名称。以下是数据帧lis设置方式的示例代码:

df <- data.frame(id = c(1,2,3),
                 country = c("Cote d'Ivoire Africa Developing Economies West Africa",
                              "South Africa United Kingdom Africa BRICS Countries",
                             "Myanmar Gambia Bangladesh Netherlands Africa Asia"))
df您可以做:

library(stringi)
vec <- stri_trans_general(countrycode::codelist$country.name.en, id = "Latin-ASCII")
stri_extract_all(df$country,regex = sprintf(r"(\b(%s)\b)",stri_c(vec,collapse = "|")))
[[1]]
[1] "Cote d'Ivoire"

[[2]]
[1] "South Africa"   "United Kingdom"

[[3]]
[1] "Gambia"      "Bangladesh"  "Netherlands"
库(stringi)

这是可能的,但是你可以选择多个这样的国家。嘿,谢谢你的快速回复。你能具体说明这是怎么可能的吗?谢谢取决于你是否有一个包含多个单词的国家名称列表,如我问题开头的示例所示;e、 g.“南非联合王国非洲金砖国家”科特迪瓦怎么样?这是一个国家吗?非常感谢。这很有效。一个小音符。缺少一个“=”。它必须是:
stri_extract_all(df$country,regex=sprintf(r=“(\b(%s)\b)”,stri_c(vec,collapse=“|”)
我刚刚在您的解决方案中发现了一个问题,我还没有设法解决。文本字符串中既有Nigeria也有Niger,所有Nigeria字符串都以Niger结尾(看起来像是提取了第一个可能的匹配项)。有什么办法解决这个问题吗?@Lisa,你添加了一个额外的
=
,你应该按照上面发布的那样做。没有丢失的
=
。请注意,对于您的回复@onyanbu,如果我这样做,我会在R中得到以下错误消息:错误:“stri_extract_all(df$country,regex=sprintf(R)(\b(%s)\b)”中出现意外的字符串常量。同时,我使用countrycode包找到了一个解决方案(请参阅更新的问题)@Lisa,看来您使用的是旧版本的R。将其改为
sprintf('\\b(%s)\\b',…)
。dotts的意思是维护所有其他东西
library(maps)
library(stringr)
all_countries <- str_c(unique(world.cities$country.etc), collapse = "|")
df$c1 <- sapply(str_extract_all(df$country, all_countries), toString)
library(countrycode)
countries <- data.frame(countryname_dict)
countries$continent <- countrycode(sourcevar = countries[["country.name.en"]],
                                   origin = "country.name.en",
                                   destination = "continent")

africa <- countries[ which(countries$continent=='Africa'), ]

library(stringr)
pat <- paste0("\\b", paste(africa$country.name.en , collapse="\\b|\\b"), "\\b")
df$country_list <- str_extract_all(df$country, regex(pat, ignore_case = TRUE))
library(stringi)
vec <- stri_trans_general(countrycode::codelist$country.name.en, id = "Latin-ASCII")
stri_extract_all(df$country,regex = sprintf(r"(\b(%s)\b)",stri_c(vec,collapse = "|")))
[[1]]
[1] "Cote d'Ivoire"

[[2]]
[1] "South Africa"   "United Kingdom"

[[3]]
[1] "Gambia"      "Bangladesh"  "Netherlands"