用字符串向量映射模式向量

map pattern vector with string vector

我想找到字符串向量中模式向量元素的第一次出现,并获得这些字符串的输出向量,其中不匹配项应分配给 NA。此外,我想针对此问题使用紧凑的矢量化解决方案(最好是 tidyverse 解决方案)。

library(stringr)
library(purrr)

示例:

patterns1 <- c("101", "102", "103", "101")
patterns2 <- c("101", "102", "103", "999", "101")
strings <- c("101a", "101a", "a102a", "aa103a")

对于 patterns1 这是可行的,因为每个元素都存在于 strings:

map_chr(patterns1, function(x) detect(strings, str_detect, x))
# [1] "101a"   "a102a"  "aa103a" "101a"

但是 patterns2 map_chr 给出了一个错误:

map_chr(patterns2, function(x) detect(strings, str_detect, x))
# Error: Result 4 is not a length 1 atomic vector

因为detectreturnsNULL如果检测失败。或者您是否建议使用 map 而不是 map_chr 的解决方法并将 NULL 元素转换为 NA?

map(patterns2, function(x) detect(strings, str_detect, x))
# [[1]]
# [1] "101a"
#
# [[2]]
# [1] "a102a"
#
# [[3]]
# [1] "aa103a"
#
# [[4]]
# NULL
#
# [[5]]
# [1] "101a"

我们可以创造条件

map_chr(patterns2, ~ detect(strings, str_detect, .x) %>% 
                               if(length(.) > 0) . else NA)
#[1] "101a"   "a102a"  "aa103a" NA       "101a"  

或与 NA 连接并取 first

map_chr(patterns2, ~ c(detect(strings, str_detect, .x), NA)[1])
#[1] "101a"   "a102a"  "aa103a" NA       "101a"