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!
You could also just paste the columns together
df$city <- ifelse(grepl("Bangkok", paste(df$location,df$state)),"Bangkok","")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With