如何识别字符串中提到的所有国家名称并进行相应拆分?
How to identify all country names mentioned in a string and split accordingly?
我有一个包含国家和其他地区名称的字符串。我只对国家名称感兴趣,理想情况下希望添加几列,每列都包含字符串中列出的国家名称。这是数据框 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"))
如果我只用 space 分割字符串,那些包含 space 的国家就会丢失(例如“英国”)。看这里:
df2 <- separate(df, country, paste0("C",3:8), sep=" ")
因此,我尝试使用 world.cities 数据集查找国家名称。但是,这似乎只是循环遍历字符串,直到出现非国家名称为止。看这里:
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)
我想知道是否可以使用 space 作为定界符但定义例外(如“英国”)。这显然可能需要一些手动工作,但对我来说似乎是最可行的解决方案。有谁知道如何定义此类异常?我当然也乐于接受并感谢任何其他解决方案。
更新:
我想出了另一个使用国家代码包的解决方案:
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"
我有一个包含国家和其他地区名称的字符串。我只对国家名称感兴趣,理想情况下希望添加几列,每列都包含字符串中列出的国家名称。这是数据框 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"))
如果我只用 space 分割字符串,那些包含 space 的国家就会丢失(例如“英国”)。看这里:
df2 <- separate(df, country, paste0("C",3:8), sep=" ")
因此,我尝试使用 world.cities 数据集查找国家名称。但是,这似乎只是循环遍历字符串,直到出现非国家名称为止。看这里:
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)
我想知道是否可以使用 space 作为定界符但定义例外(如“英国”)。这显然可能需要一些手动工作,但对我来说似乎是最可行的解决方案。有谁知道如何定义此类异常?我当然也乐于接受并感谢任何其他解决方案。
更新:
我想出了另一个使用国家代码包的解决方案:
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"