I'm having a dataframe with 22 columns where 17th column are city names stated in german. I do have another dataframe with 2 columns, in 1st - citynames in german, column 2 -in english. How do i replace values in column 17 in first dataframe using second "dictionary" dataframe?
ticket_id <- c(1,3,4,5)
depart_city <- c(Lemberg, Köln, Düsseldorf, Lemberg)
price <- c(20,25,22,21)
tickets <- data.frame(ticket,id, depart_city, price)
de <- c(Lemberg, Düsseldorf, Köln)
eng <- c(Lviv, Duesseldorf, COlogne)
dict <- data.frame(eng,de)
These are the examples of 2 dataframes.
CodePudding user response:
I would use factor
to set the levels and labels of the column of interest:
#before change
tickets$depart_city
#> [1] "Lemberg" "Köln" "Düsseldorf" "Lemberg"
#how to change
tickets$depart_city <- as.character(
factor(
tickets$depart_city,
levels = dict$de,
labels = dict$eng
)
)
#after change
tickets$depart_city
#> [1] "Lviv" "COlogne" "Duesseldorf" "Lviv"