我有一个字符串列表如下:
tofind<-c("aaa","bbb","ccc","ddd")
我还有一个矢量如下:
n<-c("aaabbb","aaa","aaacccddd","eee")
我想查找tofind
字符串的所有匹配项,以便输出结果为:
aaa,bbb
aaa
aaa,ccc,ddd
我想我可以使用str_extract_all
,但它没有给出我预期的输出
library(stringr)
sapply(n, function(x) str_extract_all(n,tofind)
如何获得预期的输出?
答案 0 :(得分:3)
你可以创建一个正则表达式:
tofind <- paste(c("aaa","bbb","ccc","ddd"), collapse="|")
str_extract_all(n, tofind)
[[1]] [1] "aaa" "bbb" [[2]] [1] "aaa" [[3]] [1] "aaa" "ccc" "ddd" [[4]] character(0)
答案 1 :(得分:1)
str_detect
功能可以在这里提供帮助
suppressPackageStartupMessages(library(tidyverse))
library(stringr)
tofind <- c("aaa", "bbb", "ccc", "ddd")
n <- c("aaabbb", "aaa", "aaacccddd", "eee")
sapply(n, function(x) tofind[str_detect(x, tofind)], USE.NAMES = FALSE)
#> [[1]]
#> [1] "aaa" "bbb"
#>
#> [[2]]
#> [1] "aaa"
#>
#> [[3]]
#> [1] "aaa" "ccc" "ddd"
#>
#> [[4]]
#> character(0)
# or the tidyverse alternative...
n %>%
map(function(x, y) y[str_detect(x, y)], tofind)
#> [[1]]
#> [1] "aaa" "bbb"
#>
#> [[2]]
#> [1] "aaa"
#>
#> [[3]]
#> [1] "aaa" "ccc" "ddd"
#>
#> [[4]]
#> character(0)
答案 2 :(得分:0)
在基地R
sapply(n, function(x) tofind[sapply(tofind, function(y) grepl(pattern = y, x = x))])
#$aaabbb
#[1] "aaa" "bbb"
#$aaa
#[1] "aaa"
#$aaacccddd
#[1] "aaa" "ccc" "ddd"
#$eee
#character(0)