【发布时间】:2015-09-22 10:49:18
【问题描述】:
我正在尝试查找名为“values”的字符串是否包含来自两个不同列表的子字符串。这是我当前的代码:
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 <- ("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 是否包含两个列表中的某些元素组合,以便以后使用。这可以一步完成吗:像这样:
for (i in 1:length(value)){
if (str_detect(value[i],(city[j]))) == TRUE && str_detect(value[i],(school[j]))) == TRUE){
.............................................
}
}
【问题讨论】:
-
你能举一些值的例子吗
-
我在上面的问题中举了一些例子
标签: r nested-loops string-matching stringr