基本上我正在寻找一个函数,它将一个字符串向量和一个搜索词作为输入,并输出一个布尔向量。在此之后,我还想获取一个字符串列表并通过相同的函数运行它以输出多个结果向量,每个字符串一个。
所以初始数据看起来像:
> searchVector <- cbind(c("aaa1","aaa2","","bbb1,aaa1,ccc1", "ddd1,ccc1,aaa1"))
> searchVector
[,1]
[1,] "aaa1"
[2,] "aaa2"
[3,] ""
[4,] "bbb1,aaa1,ccc1"
[5,] "ddd1,ccc1,aaa1"
这就是我们希望看到的:
>findTrigger(c("aaa","bbb"),searchVector)
[aaa] [bbb]
[1,] 1 0
[2,] 1 0
[3,] 0 0
[4,] 1 1
[5,] 1 0
我做了以下尝试:
searchfunction <- function (searchTerms, searchVector) {
output = matrix( nrow = length(searchVector),
ncol = length(searchTerms),
dimnames = searchTerms)
for (j in seq(1,length(searchTerms)))
{
for (i in seq(1,length(searchVector)))
{
output[i,j]=is.numeric(pmatch(searchTerms[j], searchVector[i]))
}
}
return(as.numeric(output))
}
但我只是得到一个全1的矩阵。我对 R 很陌生,我在网上四处查看,但没有任何运气。任何帮助将不胜感激,谢谢!