0

Is there a method in R, to substitute the values of a vector using a dictionary (2 column dataframe with old and new value)

The only method I know is to extract the old value into a dataframe and merge it with, what I call,the dictionary (which is a two column dataframe with old and new values). Afterwards reassign the new value to the original old value. However, it seems when using merge (at least since R v4.1, the order of the x value is not maintained, so I am using join now which keeps the original order of dataframe x intact. I am thinking that there must be an easier way, I just have not found it. Hope this is understandable, I appreciate any help.

cheers Hermann

  • Have also a look at: [fast R lookup table](https://stackoverflow.com/q/59673734/10488504) – GKi Jun 14 '21 at 09:59

2 Answers2

2

You could use a named character vector as a dict for replacement by unquoting with !!! inside of dplyr::recode. If you have your "dict" stored as a two-column dataframe, then tidyr::deframe might be handy.

library(tidyverse)

x <- c("a", "b", "c")

dict <- tribble(
  ~old, ~new, 
  "a",  "d",
  "b",  "e",
  "c",  "f"
)

recode(x, !!!deframe(dict))
#> [1] "d" "e" "f"

Created on 2021-06-14 by the reprex package (v1.0.0)

Peter H.
  • 1,995
  • 8
  • 26
1

You can use match to substitute the values of a vector using a dictionary:

D$new[match(x, D$old)]
#[1] "d" "e" "f"

You can also use the names to get the new values:

L <- setNames(D$new, D$old)
L[x]
#"d" "e" "f" 

Data:

x <- c("a", "b", "c")
D <- data.frame(old = c("a", "b", "c"), new = c("d", "e", "f"))
GKi
  • 37,245
  • 2
  • 26
  • 48