I have a tibble with data. But the values of that data are suppose to be overwritten/replaced with new values. The assignment rule is also stored in a tibble. The data tibble:
library(tidyverse)
old_data <- tibble(
var1 = c(2,2,5,4,9,7,8,9,2),
var2 = c(9,2,7,5,5,8,4,9,9)
)
Tibble with information about the assignment rule:
assignment_rule <- tibble(
new = c(1,2,3,4,5,6),
old = c(2,4,5,7,8,9)
)
I know that i could solve this example by assigning each old number individually to its new number, like:
new_data <- old_data
new_data[new_data == 2] <- 1
new_data[new_data == 4] <- 2
new_data[new_data == 5] <- 3
new_data[new_data == 7] <- 4
new_data[new_data == 8] <- 5
new_data[new_data == 9] <- 6
But I am sure there is a more elegant way to do this. Especially for handling bigger data.
I would really appreciate your help.