【问题标题】:r search for a string based on keywords and check if date is between start and end dater 根据关键字搜索字符串并检查日期是否在开始日期和结束日期之间
【发布时间】:2018-10-20 03:23:35
【问题描述】:

我有一组句子,

{ cat ate rat, rat was killed, cat killed the rat, rat killed by rat}。

首先)我想搜索列 col2 中的值是否包含这些句子中的任何一个

Second ) 如果匹配,那么我想检查 Col3 中的日期是否在 col4 和 col5 中的开始日期和结束日期之间。

这是一个测试数据集

Id      Col2                Col3        Col4        Col5
1       This cat            05-09-2001  04-10-2000  09-14-2001
2       This cat ate a rat  05-04-2011  05-01-2011  05-14-2011
3       Cat was killed      02-04-2015  02-01-2015  03-12-2015
4       Cat killed the rat  10-06-2014  09-20-2014  10-11-2014
5       Rat ran away        03-12-2008  04-12-2015  04-20-2015

这是预期的输出

Id      Col2                Col3        Col4        Col5         Event
1       This cat            05-09-2001  04-10-2000  09-14-2001   No
2       Cat ate rat         05-04-2011  05-01-2011  05-14-2011   Yes
3       Cat died            02-04-2015  02-01-2015  03-12-2015   No
4       Cat killed the rat  10-06-2014  09-20-2014  10-11-2014   Yes
5       Rat ran away        03-12-2008  04-12-2015  04-20-2015   No

这就是 id 到目前为止所做的。下面的代码正在运行。我得到了我想要的结果,但这非常低效。它非常慢并且需要很长时间。特别是如果我的 df 包含 300 万行,我需要 10 天才能完成运行此代码。非常感谢任何有关解决此问题的有效方法的建议。

keywords

for (i in 1:NROW(Df)) {

         if( NROW(Df[grep(paste0(keywords, collapse = "|"), Df$Col2[i]),]) > 0) {

             if ( (Df$Col3[i] > Df$Col4[i]) & (Df$Col3[i] < Df$Col5[i]) ){
               Df$Event <- "Yes"
             } else {
               Df$Event <- "No"
             }


         }
        print(i)
      }

【问题讨论】:

  • 请使用dput()(见R标签说明)
  • 另外我要补充一点,测试数据集和预期的输出不匹配。为什么 Col2 发生了变化?

标签: r string date filter text-mining


【解决方案1】:

基本上你需要测试三个条件。

  • Col3 >= Col4
  • Col3 Col5
  • Col2 出现在关键字中

使用ifelse 或%in% 等矢量化函数来加速您的代码。

mydf <- structure(list(Id = 1:5, Col2 = c("This cat", "This cat ate a rat", 
"Cat was killed", "Cat killed the rat", "Rat ran away"), Col3 = structure(c(11451, 
15098, 16470, 16349, 13950), class = "Date"), Col4 = structure(c(11057, 
15095, 16467, 16333, 16537), class = "Date"), Col5 = structure(c(11579, 
15108, 16506, 16354, 16545), class = "Date")), .Names = c("Id", 
"Col2", "Col3", "Col4", "Col5"), row.names = c(NA, -5L), class = "data.frame")

keywords <- c("cat ate rat", "rat was killed", "cat killed the rat", "rat killed by rat")

mydf$event <- ifelse((mydf$Col3 >= mydf$Col4) & (mydf$Col3 <= mydf$Col5) 
                      & mydf$Col2 %in% keywords, "Yes", "No")

请注意,此版本区分大小写。你可能对tolower等函数感兴趣。

mydf$event <- ifelse((mydf$Col3 >= mydf$Col4) & (mydf$Col3 <= mydf$Col5) 
                     & tolower(mydf$Col2) %in% keywords, "Yes", "No")

【讨论】:

    【解决方案2】:

    简答:

    df$Event <- sapply(tolower(df$Col2), function(el) el %in% sentences)
    

    在 for 循环中做你想做的事。

    在 R 中,您必须避免 for 循环并尝试使用 apply-family 函数。 tolower 将 df$Col2 的内容变为小写。 对于此列向量的每个元素,应用了定义的函数 function(el) el %in% sentences(它询问每个元素是否是 sentences 字符向量的一部分,并且布尔结果首先被收集到一个列表中,然后,它尝试s将收集到的结果进一步简化为一个向量 (sapply)。

    完整的工作代码版本:

    数据读入和准备

    sentences <- unlist(strsplit("cat ate rat, rat was killed, cat killed the rat, rat killed by rat",", "))
    

    只是将您给定的文本更改为数据框

    txt2df <- function(dfstr) {
      lines <- unlist(strsplit(txt, "\n"))
      l <- unlist(lapply(lines,strsplit, "\ {2, }"), recursive = FALSE)
      df <- as.data.frame(Reduce(rbind, l[2:length(l)]), row.names = FALSE)
      colnames(df) <- l[[1]]
      df
    }
    

    将函数应用于多行字符串以获得data.frame:

    df <- txt2df("Id      Col2                Col3        Col4        Col5
    1       This cat            05-09-2001  04-10-2000  09-14-2001
    2       This cat ate a rat  05-04-2011  05-01-2011  05-14-2011
    3       Cat was killed      02-04-2015  02-01-2015  03-12-2015
    4       Cat killed the rat  10-06-2014  09-20-2014  10-11-2014
    5       Rat ran away        03-12-2008  04-12-2015  04-20-2015")
    
    
    df
    
      Id               Col2       Col3       Col4       Col5
    1  1           This cat 05-09-2001 04-10-2000 09-14-2001
    2  2 This cat ate a rat 05-04-2011 05-01-2011 05-14-2011
    3  3     Cat was killed 02-04-2015 02-01-2015 03-12-2015
    4  4 Cat killed the rat 10-06-2014 09-20-2014 10-11-2014
    5  5       Rat ran away 03-12-2008 04-12-2015 04-20-2015
    

    查找函数

    如果 df$Col2 值的小写字母是任何一个句子,则查找:

    df$Event <- sapply(tolower(df$Col2), function(el) el %in% sentences)
    

    结果

    df
    
      Id               Col2       Col3       Col4       Col5 Event
    1  1           This cat 05-09-2001 04-10-2000 09-14-2001 FALSE
    2  2 This cat ate a rat 05-04-2011 05-01-2011 05-14-2011 FALSE
    3  3     Cat was killed 02-04-2015 02-01-2015 03-12-2015 FALSE
    4  4 Cat killed the rat 10-06-2014 09-20-2014 10-11-2014  TRUE
    5  5       Rat ran away 03-12-2008 04-12-2015 04-20-2015 FALSE
    

    【讨论】:

      猜你喜欢
      • 2015-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-20
      • 1970-01-01
      • 2019-07-11
      相关资源
      最近更新 更多