如何在嵌套列表中执行条件搜索

2024-01-08

我有一个嵌套列表,如下所示:

list(c("Oesophagus irregular z-line as previously.", " quad biopsies at ,,,m"
), c("Normal examination", "cardia mild inflammation."
), c("stomach normal", "Small polyp EMR and completely removed", 
"Duodenum  normal", "Jejunum normal", "GOJ normal", 
"Nodule seen at the mid oesophagus normal", "This was removed by EMR", 
"All other sites normal  normal", " A small area of residual stomach was removed by APC "))

我想在每个元素中搜索是否存在从名为的列表中获取的任何术语EventList:

EventList<-c("RFA","EMR","APC")

如果找到该术语,那么我想查看位置列表中的位置是否也出现在同一个句子中:

LocationList<-function(){

  tofind <-paste(c("Stomach","Antrum","Duodenum","Oesophagus","GOJ"),collapse = "|")

  return(tofind)

}

如果没有找到,那么我想查看前面的句子,看看是否可以看到某个位置。

到目前为止,我只能查看同一句话:

r1 <-lapply(text,function(x) Map(paste, str_extract_all(tolower(x),tolower(EventList)), str_extract_all(tolower(x),tolower(LocationList())), MoreArgs = list(sep=":")))

但这只是查找每个句子中是否存在术语,如果不存在则不输出任何内容。如何将其转换为同时查看列表中的前面的句子?像这样的事情:

ifelse(lapply(text,function(x) str_extract_all(tolower(x),tolower(LocationList())),str_extract_all(tolower(x),tolower(EventList()), lag element and do the same??

预期输出:

1 ""
2 ""
3 stomach:EMR,oesophagus:EMR,stomach:APC

根据帖子中提到的条件

sapply(text,function(x) {

           x1 <- str_extract_all(tolower(x),tolower(paste(EventList, collapse="|")))
           i1 <- which(lengths(x1) > 0)
           if(any(i1)) {
             paste(unlist(Map(c, str_extract_all(tolower(x[i1-1]), 
                                         tolower(LocationList())), 
                       str_extract_all(tolower(x[i1]), tolower(LocationList())))), 
                        toupper(x1[i1]), sep=":", collapse=", ") 

           } else ""

             }

             )

#[1] ""  
#[2] ""   
#[3] "stomach:EMR, oesophagus:EMR, stomach:APC"
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在嵌套列表中执行条件搜索 的相关文章

随机推荐