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

  • Home
  • SEARCH
  • 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 6046889
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:17:27+00:00 2026-05-23T07:17:27+00:00

I currently have a dataset which has two columns that I’d like to compare.

  • 0

I currently have a dataset which has two columns that I’d like to compare. In one column, I have a string that I’d like to search for (let’s call it column A). In a second column (let’s call it column B) are some more strings.

The problem is that both columns have varying contents, so the pattern being searched for in the regular expression is likely to change from one row to another. Normally, when I’m searching a column for a particular string, I use something like this:

df$output <- NA
df$output[grep("TARGET_STRING", df$column_B)] <- "STRING_FOUND"

However, now that I’m trying to do this:

df$output[grep(df$column_A, df$column_B)] <- "STRING_FOUND"

Unfortunately, this gives an error:

argument ‘pattern’ has length > 1 and
only the first element will be used

I’ve tried various methods to fix this, and can’t seem to find a simple solution, and I’m sure there must be one. I can see why it’s throwing an error (I think), but I’m not sure how to solve it. What do I need to do to get the regular expression working?

Edit: Here’s the testing data.frame I’ve been using to explore it:

column_A <- c("A", "A", "B", "B")
column_B <- c("A", "zzz", "B", "zzz")

greptest <- data.frame(column_A, column_B)

greptest$output<-NA
greptest$output[grep(greptest$column_A, greptest$column_B)] <- "STRING_FOUND"
  • 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-05-23T07:17:28+00:00Added an answer on May 23, 2026 at 7:17 am

    You can write a function that wraps grepl and then use apply:

    grepFun <- function(rw){
        grepl(rw[1],rw[2],fixed=TRUE)
    }
    
    xx <- apply(greptest,1,grepFun)
    greptest$output[xx] <- "STRING_FOUND"
    

    You’ve already excepted my answer, but I thought I’d provide another, somewhat more efficient version using ddply:

    grepFun1 <- function(x){
        ind <- grepl(x$column_A[1],x$column_B,fixed=TRUE)
        x$output <- NA
        x$output[ind] <- "STRING_FOUND"
        x
    }
    
    ddply(greptest,.(column_A),.fun=grepFun1)
    

    This version will likely be faster if you have lots of repetition in the values for column_A.

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

Sidebar

Related Questions

I have a report with a dataset that has a column with booleans. In
I have currently a dataset in asp which contains my members details. I am
I have a DataSet with a DataTable that correctly fills a single DataRow through
I currently have an MS Access application that connects to a PostgreSQL database via
We currently have code like this: Dim xDoc = XDocument.Load(myXMLFilePath) The only way we
I currently have a functioning in-house Windows Forms application which extensively uses the DataGridView
I've got a table which has the usual ParentID, ChildID as it's first two
Hi I am currently using TableAdapter that returns a dataTable, which is fine to
Summary I have a large an rapidly changing dataset which I wish to bind
I have the WSDL for a web service that I cannot currently connect to.

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.