【问题标题】:How to detect substrings from multiple lists within a string in R如何从R中的字符串中的多个列表中检测子字符串
【发布时间】: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){
        ...........................................................
        }
      }
    }
  }
}

cityschool 是不同长度的独立向量,每个向量都包含字符串元素。

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


【解决方案1】:

试试这个:

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

当执行时,它会打印出与城市和学校有共同元素的那些:

[1] "Rey Juan Carlos Law Dept, Madrid"
[1] "New York University, Center of PoliSci Studies"
[1] "London, Mathematics"

【讨论】:

    【解决方案2】:

    这个问题与我一直在研究的类似。出于我的目的,需要返回一个保留原始输入结构的数据框。

    这对你来说可能也是正确的。因此,我将@rbm 的优秀解决方案修改如下:

    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)
    

    这会导致以下结果:

                                              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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-28
      • 1970-01-01
      • 2020-12-14
      • 2018-11-24
      • 1970-01-01
      • 2019-05-07
      相关资源
      最近更新 更多