Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8832165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:28:05+00:00 2026-06-14T08:28:05+00:00

Basically I’m looking to write a function that will take a vector of strings

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T08:28:06+00:00Added an answer on June 14, 2026 at 8:28 am

    mapply and grep or grepl (thanks joran) are your friend:

    searchTerms <- c("aaa", "bbb")
    searchVector <- cbind(c("aaa1","aaa2","","bbb1,aaa1,ccc1", "ddd1,ccc1,aaa1"))
    M <- mapply(grepl, searchTerms, MoreArgs=list(x=searchVector)) 
    M
           aaa   bbb
    [1,]  TRUE FALSE
    [2,]  TRUE FALSE    
    [3,] FALSE FALSE
    [4,]  TRUE  TRUE
    [5,]  TRUE FALSE
    

    If you want it as 1,0: apply(M,2,as.numeric)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, I have a UIImageView that will loop through 8 PNGs over 0.5 seconds.
Basically from C++ FAQ I learned that: A virtual function allows derived classes to
Basically this function is meant to store the height value of the element that
basically I have a function that resizes elements accordingly with jquery triggering the function
Basically I’ve heard that certain conditions will cause .NET to blow past the finally
Basically I've made a form in Cakephp 2.1 with a jQuery button that appends
Basically, what I have is a setInterval inside a function. What I want to
Basically I have a web service connected to a database, that has an insert
Basically what I'm doing is building a desktop application that needs to connect to
Basically the code inside the displayCharityList () works. I decided to put that code

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.