Note that there are some explanatory texts on larger screens.

plurals
  1. PORegular Expressions in R - compare one column to another
    text
    copied!<p>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.</p> <p>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:</p> <pre><code>df$output &lt;- NA df$output[grep("TARGET_STRING", df$column_B)] &lt;- "STRING_FOUND" </code></pre> <p>However, now that I'm trying to do this:</p> <pre><code>df$output[grep(df$column_A, df$column_B)] &lt;- "STRING_FOUND" </code></pre> <p>Unfortunately, this gives an error: </p> <blockquote> <p>argument 'pattern' has length > 1 and only the first element will be used</p> </blockquote> <p>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?</p> <p><strong>Edit:</strong> Here's the testing data.frame I've been using to explore it:</p> <pre><code>column_A &lt;- c("A", "A", "B", "B") column_B &lt;- c("A", "zzz", "B", "zzz") greptest &lt;- data.frame(column_A, column_B) greptest$output&lt;-NA greptest$output[grep(greptest$column_A, greptest$column_B)] &lt;- "STRING_FOUND" </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload