匹配字符串regex完全匹配

时间:2020-06-21 07:29:02

标签: r regex string stringi

此线程遵循以下已回答的问题:Matching strings loop over multiple columns

我打开了一个新线程,因为我只想对完全匹配的标志进行更新。

我在单独的列中有一个关键字表,如下所示:

#codes table
codes <- structure(
  list(
    Support = structure(
      c(2L, 3L, NA),
      .Label = c("",
                 "help", "questions"),
      class = "factor"
    ),
    Online = structure(
      c(1L,
        3L, 2L),
      .Label = c("activities", "discussion board", "quiz", "sy"),
      class = "factor"
    ),
    Resources = structure(
      c(3L, 2L, NA),
      .Label = c("", "pdf",
                 "textbook"),
      class = "factor"
    )
  ),
  row.names = c(NA,-3L),
  class = "data.frame"
)

我还有一个注释表,其结构如下:

#comments table
comments <- structure(
  list(
    SurveyID = structure(
      1:5,
      .Label = c("ID_1", "ID_2",
                 "ID_3", "ID_4", "ID_5"),
      class = "factor"
    ),
    Open_comments = structure(
      c(2L,
        4L, 3L, 5L, 1L),
      .Label = c(
        "I could never get the pdf to download",
        "I could never get the system to work",
        "I didn’t get the help I needed on time",
        "my questions went unanswered",
        "staying motivated to get through the textbook",
        "there wasn’t enough engagement in the discussion board"
      ),
      class = "factor"
    )
  ),
  class = "data.frame",
  row.names = c(NA,-5L)
)

我要做什么:

搜索完全匹配关键字。 @Len Greski和@Ronak Shah在上一个线程中提供了以下工作代码(非常感谢双方):

resultsList <- lapply(1:ncol(codes),function(x){
     y <- stri_detect_regex(comments$Open_comments,paste(codes[[x]],collapse = "|"))
     ifelse(y == TRUE,1,0)   
     })

results <- as.data.frame(do.call(cbind,resultsList))
colnames(results) <- colnames(codes)
mergedData <- cbind(comments,results)
mergedData

comments[names(codes)] <- lapply(codes, function(x) 
            +(grepl(paste0(na.omit(x), collapse = "|"), comments$Open_comments)))

两者都很好,但是我遇到了麻烦,现在需要完全匹配关键字。根据上面的示例表,如果我有一个关键字“ sy”,则代码将标记带有“ system”一词的任何注释。我将修改以上任一代码段,以在只有“ sy”个完全匹配项的地方标记注释。

非常感谢

0 个答案:

没有答案