'Automatically recode variable values based on possible order of occurrence in R

I am working on processing data from multiple surveys in R. These surveys contain the same questions completed in different languages. For some of the questions, selections that indicate the same response have unfortunately been saved under a different value, and I want to recode these so that they are the same across surveys.

For instance, participants have to select the country that they live in (out of 195 options). In the English version this looks like this:

Country selected in the survey Stored as value in the dataset
Afghanistan 1
Albania 2
Algeria 3
[…] [...]
Zimbabwe 195

In the Italian version, the same variable looks like this:

Country selected in the survey Stored as value in the dataset
Afghanistan 2
Albania 198
Algeria 199
[…] [...]
Zimbabwe 391

In other words, the order of the countries/values is the same, but the saved values don’t match between English + Italian.

I can manually recode each value (i.e., in the Italian version recode 2 as 1, 198 as 2, 199 as 3, etc.) but it takes a lot of time to go through 195 options for multiple surveys. Is there some way that I can automate this, so that the smallest possible value in the Italian version (in this example, value 2) gets recoded to 1, and the largest possible value (in this example, value 391) gets recoded to 195, and everything in between gets recoded accordingly automatically as well?

(Please note this is about recoding the possible values, as the dataset does not necessarily contain a value from each response option.)

Thank you!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source