[英]Finding all string matches from another dataframe in R
I am relatively new in R.我在 R 中相对较新。
I have a dataframe locs
that has 1 variable V1
and looks like:我有一个数据框
locs
,它有 1 个变量V1
,看起来像:
V1
edmonton general hospital
cardiovascular institute, hospital san carlos, madrid spain
hospital of santa maria, lisbon, portugal
and another dataframe cities
that has two variables that look like this:另一个数据框
cities
有两个如下所示的变量:
city country
edmonton canada
san carlos spain
los angeles united states
santa maria united states
tokyo japan
madrid spain
santa maria portugal
lisbon portugal
I want to create two new variables in locs
that relates any string match of V1
within city
so that locs
looks like this:我想在
locs
中创建两个新变量,这些变量将city
内V1
任何字符串匹配相关联,以便locs
如下所示:
V1 city country
edmonton general hospital edmonton canada
hospital san carlos, madrid spain san carlos, madrid spain
hospital of santa maria, lisbon, portugal santa maria, lisbon portugal, united states
A few things to note: V1
may have multiple country names.需要注意的几点:
V1
可能有多个国家/地区名称。 Also, if there is a repeat country (for instance, both san carlos and madrid are in spain), then I only want one instance of the country.另外,如果有一个重复的国家(例如,圣卡洛斯和马德里都在西班牙),那么我只想要该国家的一个实例。
Please advise.请指教。
Thanks.谢谢。
A solution using tidyverse
and stringr
.使用
tidyverse
和stringr
解决方案。 locs2
is the final output. locs2
是最终输出。
library(tidyverse)
library(stringr)
locs2 <- locs %>%
rowwise() %>%
mutate(city = list(str_match(V1, cities$city))) %>%
unnest() %>%
drop_na(city) %>%
left_join(cities, by = "city") %>%
group_by(V1) %>%
summarise_all(funs(toString(sort(unique(.)))))
Result结果
locs2 %>% as.data.frame()
V1 city country
1 cardiovascular institute, hospital san carlos, madrid spain madrid, san carlos spain
2 edmonton general hospital edmonton canada
3 hospital of santa maria, lisbon, portugal lisbon, santa maria portugal, united states
DATA数据
library(tidyverse)
locs <- data_frame(V1 = c("edmonton general hospital",
"cardiovascular institute, hospital san carlos, madrid spain",
"hospital of santa maria, lisbon, portugal"))
cities <- read.table(text = "city country
edmonton canada
'san carlos' spain
'los angeles' 'united states'
'santa maria' 'united states'
tokyo japan
madrid spain
'santa maria' portugal
lisbon portugal",
header = TRUE, stringsAsFactors = FALSE)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.