Thank you for looking at my question, and happy new year!
My problem/question: I have a dataframe column containing a list of names like this (some also repeating):
Name (German)
Josef
Georg
Mathilde
Josef
Ludwig
Lorenz
Georg
...
And I want to e.g. convert these names to their English counterpart, with the corresponding names in German and English being contained in another reference file/dataframe like this:
Name (German) Name (English)
Mathilde Mathilda
Georg George
Lorenz Lawrence
Josef Joseph
Ludwig Lewis
...
So that at the end, my dataframe with a new column would look like this:
Name (German) Name (English)
Josef Joseph
Georg George
Mathilde Mathilda
Josef Joseph
Ludwig Lewis
Lorenz Lawrence
Georg George
...
If anyone knows how to accomplish this, I would be very grateful if you could help me figure out how to do it. In any case, thank you for any help!
With best regards!
CodePudding user response:
Writing a dictionary using read.table().
dict <- read.table(header=TRUE, text='
German English
Mathilde Mathilda
Georg George
Lorenz Lawrence
Josef Joseph
Ludwig Lewis
')
and then match().
transform(dat, English=dict[match(dat$Name, dict$German), ]$English)
# Name English
# 1 Josef Joseph
# 2 Georg George
# 3 Mathilde Mathilda
# 4 Josef Joseph
# 5 Ludwig Lewis
# 6 Lorenz Lawrence
# 7 Georg George
Data:
dat <- structure(list(Name = c("Josef", "Georg", "Mathilde", "Josef",
"Ludwig", "Lorenz", "Georg")), class = "data.frame", row.names = c(NA,
-7L))
