Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using grepl to extract a value from text across multiple columns

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:

location          state
Bangkok
                  Bangkok Metropolitan
Central Thai      Bangkok 

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:

df$city <- ifelse(grepl("Bangkok",df$location),"Bangkok","")

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

df$city <- ifelse(grepl("Bangkok",df$location||df$state),"Bangkok","")

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

Any help appreciated. Thanks!

like image 248
RichS Avatar asked Dec 10 '25 00:12

RichS


1 Answers

You could also just paste the columns together

df$city <- ifelse(grepl("Bangkok", paste(df$location,df$state)),"Bangkok","")
like image 123
jMathew Avatar answered Dec 12 '25 17:12

jMathew



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!