r - 如何在purrr中获得最接近参考词的词
问题描述
我有一个清单如下:
list(c("\n", "\n", "oesophagus graded and fine\n",
"\n", "\n", "\n", "stomach and antrum altough with some rfa response rfa\n",
"\n", "mucosa washed a lot\n", "\n", "treated with halo rfa ultra \n",
"\n", "total of 100 times\n", "\n", "duodenum looks ok"))
我想从一个列表中提取与另一个列表中的另一个词最接近的词。
我想要的输出是
antrum:rfa
我的第一个清单是:
EventList<-c("rfa", "apc", "dilat", "emr", "clip", "grasp", "probe", "iodine",
"acetic", "nac", "peg", "botox")
我的第二个清单是:
tofind<-"ascending|descending|sigmoid|rectum|transverse|caecum|splenic|ileum|rectosigmoid|ileocaecal|hepatic|colon|terminal|terminal ileum|ileoanal|prepouch|pouch|stomach|antrum|duodenum|oesophagus|goj|ogj|cardia|anastomosis"
我正在使用的代码是:
EventList %>%
map(
~words %>%
str_which(paste0('^.*', .x)) %>%
map_chr(
~words[1:.x] %>%
str_c(collapse = ' ') %>%
str_extract_all(regex(tofind, ignore_case = TRUE)) %>%
map_if(is_empty, ~ NA_character_) %>%
flatten_chr()%>%
`[[`(1) %>%
.[length(.)]
) %>%
paste0(':', .x)
) %>%
unlist() %>%
str_subset('.+:')
这给了我事件(在这种情况下rfa
),但不是将它分配给antrum
,而是将它分配给oesophagus
。
因此,它将它赋予tofind
列表中找到的第一个术语,而不是最接近事件的术语。
我怀疑这条线
`[[`(1) %>%
.[length(.)]
是罪魁祸首,但我不知道如何更改它,以便它给我最接近的术语而不是第一个术语
解决方案
以下为您提供了匹配中tofind
每个匹配元素的最后一个元素EventList
map(EventList,
function(event) {
indices <- map(words, str_which, pattern = event)
map(indices, function(i)
map2_chr(words, i, ~ .x[seq_len(.y)] %>%
str_c(collapse = ' ') %>%
str_extract_all(regex(tofind, ignore_case = TRUE), simplify = TRUE) %>%
last()) %>%
map_if(is_empty, ~ NA_character_)
) %>%
unlist() %>%
paste0(':', event)
}) %>%
unlist() %>%
str_subset('.+:')
# [1] "antrum:rfa" "oesophagus:rfa"
推荐阅读
- amazon-web-services - 如何约束客户端在 s3 上传时发送正确的 sha256 作为文件密钥?(预签名网址)
- java - 识别无效数据并通过控制台上的消息向用户报告
- java - Java 当前日期和过去日期之间的年、月、日、小时、分钟、秒之间的差异
- r - 将值从一个表复制到另一个表,仅当第二个表具有特定值时
- maven - sbt:在使用凭据获取私有 maven 存储库时添加 http 标头
- sql - 执行以下查询时 Netezza Box 重新启动
- php - 按条件从数组中删除子数组
- jquery - 如何让 Ajax 加载/显示数据并返回变量
- mysql - mysql设置当前用户为默认值
- spring - 如何重试失败的操作?