有一个函数可以在R中的字符串之间进行匹配吗?

时间:2017-10-24 14:09:27

标签: r

我问的是一个允许轻松做某事的功能

grepl(c("word1","word2"), vector_of_strings)

我的意思是,做类似的事情:"在这个载体的某个位置有这些词?" TRUE/FALSE

由于

2 个答案:

答案 0 :(得分:0)

我想你想要像android:layout_gravity="center"

这样的东西
stringr::str_detect

答案 1 :(得分:0)

可能是这样的:

vector_of_strings <- c(
    'here is word1',
    'and word2 plus word4',
    'but word3 and word1 are here'
)

sapply(c("word1", "word2", "word3", "word4"), function(x){
    grepl(x, vector_of_strings)
})

##      word1 word2 word3 word4
## [1,]  TRUE FALSE FALSE FALSE
## [2,] FALSE  TRUE FALSE  TRUE
## [3,]  TRUE FALSE  TRUE FALSE

其中返回矩阵的每一行对应于字符串向量中的元素。