How to detect substrings from multiple lists within a string in R

天大地大妈咪最大 提交于 2019-12-11 04:09:22

问题


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 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", ..........)

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){
                   .............................................
    }
}

回答1:


Try this:

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"



回答2:


This problem is similar one I have been working on. For my purposes, having a dataframe returned that retains the structure of the original input is needed.

This may be true for you too. I have thus amended the excellent solution from @rbm as follows:

library("stringr")

cityList <- c("Madrid", "London", "Paris", "Sofia", "Cairo", "Detroit", "New York")
schoolList <- c("Law", "Mathematics", "PoliSci", "Economics")
valueList <- c(
  "Rey Juan Carlos Law Dept, Madrid",
  "New York University, Center of PoliSci Studies",
  "Los Angeles, CALTECH",
  "London, Physics",
  "London, Mathematics"
)


df <- data.frame(value, city=NA, school=NA, stringsAsFactors = FALSE)

i = 0
for (v in value)
{
  i = i + 1
  if (sum(str_detect(v, cityList)) > 0 & sum(str_detect(v, schoolList)) > 0)
  {
    df$city[i] <- schoolList[[which(str_detect(v, schoolList))]]
    df$school[i] <- cityList[[which(str_detect(v, cityList))]]
  } else {
    df$city[i] <- ""
    df$school[i] <- ""
  }
}
print(df)

This results in the following:

                                          value        city   school
1               Rey Juan Carlos Law Dept, Madrid         Law   Madrid
2 New York University, Center of PoliSci Studies     PoliSci New York
3                           Los Angeles, CALTECH                     
4                                London, Physics                     
5                            London, Mathematics Mathematics   London


来源:https://stackoverflow.com/questions/32714927/how-to-detect-substrings-from-multiple-lists-within-a-string-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!