A have two data frame.
df1<- data.frame(code=c("90936","NA","90903","NA","90936","909363","90903","90936","90936"),city=c("cityA","cityB","cityC","cityD","cityF","cityY","cityP","cityA","cityF"))
Note that some city names are different but have the same code
df2<- data.frame(code=c("90936","90903","90936","909363","90903"),city2=c("cityA","cityC","cityF","cityY","cityP"))
Second data frame have the true city names and codes.
I want to write a loop which checks if "codes" in two data frame matches create a new column named "place" in df1 and copies the the city name in df2$city2 to place column with condition:
if the codes in two dataframe matches copy the value in city2 to place column if there is a code in df1 but doesn't match with the code in df2, it will write to place column "unknown"
if the code in df1 is NA it should stay as NA
I need a quick help it is very important for me. Thank you in advance
Aucun commentaire:
Enregistrer un commentaire