I have following dataframes in R, How can I substitute the test_data$origin_country and test_data$destin_country with the numerical values at country_codes$ID?
test_data <- data.frame(
origin_country = c('US', 'US', 'DE', 'CN'),
destin_country = c('DE', 'DE', 'UK', 'IT'),
year = c(2020, 2020, 2019, 2019),
item = c('wheat', 'wheat', 'wheat', 'rice'),
value = c(2000, 2000, 3000, 2500))
country_codes <- data.frame(
countries = c('CN', 'DE', 'IT', 'UK', 'US'),
ID = c(1, 2, 3, 4, 5))
I have seen very similar questions, but none tackling this case. My desired result is:
output <- data.frame(
origin_country = c('5', '5', '2', '1'),
destin_country = c('2', '2', '4', '3'),
year = c(2020, 2020, 2019, 2019),
item = c('wheat', 'wheat', 'wheat', 'rice'),
value = c(2000, 2000, 3000, 2500))
Truly appreciate your insights!