【发布时间】:2019-11-05 10:08:48
【问题描述】:
我在location 列中有一个包含国家和城市的data.frame,我想通过匹配来自library(maps)(或任何其他国家名称集合)的world.cities$country.etc 数据框来提取前者。
考虑这个例子:
df <- data.frame(location = c("Aarup, Denmark",
"Switzerland",
"Estonia: Aaspere"),
other_col = c(2,3,4))
我尝试使用此代码
df %>% extract(location,
into = c("country", "rest_location"),
remove = FALSE,
function(x) x[which x %in% world.cities$country.etc])
但我没有成功;我期待这样的事情:
location other_col country rest_location
1 Aarup, Denmark 2 Denmark Aarup,
2 Switzerland 3 Switzerland
3 Estonia: Aaspere 4 Estonia : Aaspere
【问题讨论】: