Nazar Duma
Nazar Duma

Reputation: 583

Replace values in specific column of dataframe using dictionary dataframe in R

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.

Upvotes: 0

Views: 230

Answers (1)

AndS.
AndS.

Reputation: 8110

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"

Upvotes: 1

Related Questions