[英]R: Find vector elements containing multiple string matches
I would like to find the elements of a vector (strings) which contain all of the strings specified by another vector. 我想找到一个向量(字符串)的元素,其中包含另一个向量指定的所有字符串。 For example,
例如,
x <- c("xxxabcxdxexfxx", "xxaxbcdexx", "xaxxxbc")
a <- c("a", "b", "c", "d", "e", "f")
I would like to find the elements of x
that contain all the strings in a
, so to get 我想找到的元素
x
包含在所有的字符串a
,因此要获得
[1] TRUE FALSE FALSE
sapply(x, function(string) all(Vectorize(grepl)(pattern = a, x = string)))
#xxxabcxdxexfxx xxaxbcdexx xaxxxbc
# TRUE FALSE FALSE
OR 要么
rowSums(sapply(a, function(P) grepl(P, x))) == length(a)
#[1] TRUE FALSE FALSE
OR 要么
grepl(pattern = paste(sort(a), collapse = ""),
x = sapply(strsplit(x, ""),
function(x) paste(sort(x), collapse = "")))
#[1] TRUE FALSE FALSE
OR 要么
lengths(sapply(strsplit(x,""), setdiff, x = a)) == 0
#[1] TRUE FALSE FALSE
另一个:
sapply(strsplit(x,""), function(y) all(a %in% y))
Using gregexpr
: 使用
gregexpr
:
lengths(gregexpr(pattern = paste(a, collapse = "|"), text = x)) == length(a)
# [1] TRUE FALSE FALSE
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.