如何在嵌套列表中执行条件搜索 [英] How to perform conditional search within a nested list

查看:28
本文介绍了如何在嵌套列表中执行条件搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个嵌套列表如下:

I have a nested list as follows:

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 的列表中提取的任何术语的存在:

I would like to search in each element for the presence of any term taken from a list called EventList:

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

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

If the term is found then I would like to see if a location from a locaation list, is also present in the same sentence:

LocationList<-function(){

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

  return(tofind)

}

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

If it is not found then I'd like to look in the preceding sentence to see if a location can be seen.

到目前为止,我只能看到同一个句子:

So far I have been able to look in the same sentence only:

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

但这只是寻找每个句子中术语的存在,如果不存在则不输出任何内容.如何将其转换为也可以查看列表中的前一句?像这样:

but this just looks for the presence of the terms in each sentence and outputs nothing if not present. How do I convert this to also look in the preceding sentence in a list? Someting like this:

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"

这篇关于如何在嵌套列表中执行条件搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆