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!
Aucun commentaire:
Enregistrer un commentaire