Using grepl to extract a value from text across multiple columns
<p>I have a dataframe (df) containing 2 columns of data for state and city. Sometimes, however, the data inside the 2 columns are transposed or incorrectly entered. The dataframe will look something like this:</p>

<pre><code>location          state
Bangkok
                  Bangkok Metropolitan
Central Thai      Bangkok 
</code></pre>

<p>I want to create a new column, "City" by extracting 'Bangkok' from these two into a separate column. I can do this for one column by something like:</p>

<pre><code>df$city &lt;- ifelse(grepl("Bangkok",df$location),"Bangkok","")
</code></pre>

<p>However, I want to search at least 2 or more columns at once, something like:</p>

<pre><code>df$city &lt;- ifelse(grepl("Bangkok",df$location||df$state),"Bangkok","")
</code></pre>

<p>which, obviously, doesn't work. 'filter' in plyr I think does something similar but in reverse.</p>

<p>Any help appreciated. Thanks!</p>