如何从R中的字符串中的多个列表中检测子字符串 [英] How to detect substrings from multiple lists within a string in R
本文介绍了如何从R中的字符串中的多个列表中检测子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试查找名为"values"的字符串是否包含来自两个不同列表的子字符串.这是我当前的代码:
I am attempting to try and find if a string called "values" contains substrings from two different lists. This is my current code:
for (i in 1:length(value)){
for (j in 1:length(city)){
if (str_detect(value[i],(city[j]))) == TRUE){
for (k in 1:length(school)){
if (str_detect(value[i],(school[j]))) == TRUE){
...........................................................
}
}
}
}
}
city
和school
是长度不同的单独向量,每个向量都包含字符串元素.
city
and school
are separate vectors of different length, each containing string elements.
city <- ("Madrid", "London", "Paris", "Sofia", "Cairo", "Detroit", "New York")
school <- ("Law", "Mathematics", "PoliSci", "Economics")
value <- ("Rey Juan Carlos Law Dept, Madrid", "New York University, Center of PoliSci Studies", ..........)
我想做的是查看value
是否包含来自两个列表的元素的某种组合,以供以后使用.可以一步完成吗:像这样:
What I want to do is see if value
contains some combination of elements from both lists to later work with that. Can this be done in a single step: something like this:
for (i in 1:length(value)){
if (str_detect(value[i],(city[j]))) == TRUE && str_detect(value[i],(school[j]))) == TRUE){
.............................................
}
}
推荐答案
尝试一下:
library("stringr")
city <- c("Madrid", "London", "Paris", "Sofia", "Cairo", "Detroit", "New York")
school <- c("Law", "Mathematics", "PoliSci", "Economics")
value <- c(
"Rey Juan Carlos Law Dept, Madrid",
"New York University, Center of PoliSci Studies",
"Los Angeles, CALTECH",
"London, Physics",
"London, Mathematics"
)
for (v in value)
{
if (sum(str_detect(v, city)) > 0 & sum(str_detect(v, school)) > 0)
{
print (v)
}
}
执行时,将打印与城市和学校具有共同元素的内容:
when executed it will print those which have common element with city and school:
[1] "Rey Juan Carlos Law Dept, Madrid"
[1] "New York University, Center of PoliSci Studies"
[1] "London, Mathematics"
这篇关于如何从R中的字符串中的多个列表中检测子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文