我有一个嵌套列表,如下所示:
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
答案 0 :(得分:1)
根据帖子中提到的条件
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"