Search code examples
rreplaceassignment-operator

How to replace data in tibble with new values when the assignment rule is stored in another tibble?


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.


Solution

  • Use a named vector to do the matching and recoding

    library(dplyr)
    library(tibble)
    new_data <- old_data  %>%
        mutate(across(everything(),
              ~deframe(assignment_rule[2:1])[as.character(.)]))
    

    -output

    new_data
    # A tibble: 9 × 2
       var1  var2
      <dbl> <dbl>
    1     1     6
    2     1     1
    3     3     4
    4     2     3
    5     6     3
    6     4     5
    7     5     2
    8     6     6
    9     1     6