Basically I’m looking to write a function that will take a vector of strings and a search term as input, and output a boolean vector. After this, I’d also like to take a list of strings and run it through this same function to output multiple results vectors, one for each string.
So the initial data looks like:
> 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"
and this is what we’d hope to see:
>findTrigger(c("aaa","bbb"),searchVector)
[aaa] [bbb]
[1,] 1 0
[2,] 1 0
[3,] 0 0
[4,] 1 1
[5,] 1 0
I’ve made the following attempt:
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))
}
But I just get a matrix of all 1’s. I’m fairly new to R and I’ve looked around online, but haven’t had any luck. Any help would be greatly appreciated, Thanks!
mapplyandgreporgrepl(thanks joran) are your friend:If you want it as
1,0:apply(M,2,as.numeric)