2

A relatively trivial question that has been bothering me for a while, but to which I have not yet found an answer - perhaps because I have trouble verbalizing the problem for search engines.

Here is a column of a data frame that contains identifiers.

data <- data.frame("id" = c("D78", "L30", "F02", "A23", "B45", "T01", "Q38", "S30", "K84", "O04", "P12", "Z33")) 

Based on a lookup table, outdated identifiers are to be recoded into new ones. Here is an example look up table.

recode_table <- data.frame("old" = c("A23", "B45", "K84", "Z33"),
                           "new" = c("A24", "B46", "K88", "Z33"))

What I need now can be done with a merge or a loop. Here a loop example:

for(ID in recode_table$old) {
  data[data$id == ID, "id"] <- recode_table[recode_table$old == ID, "new"]
}

But I am looking for a dplyr solution without having to use the " join" family. I would like something like this.

    data <- mutate(data, id = ifelse(id %in% recode_table$old, filter(recode_table, old == id) %>% pull(new), id))

Obviously though, I can't use the column name ("id") of the table in order to identify the new ID. References to corresponding passages in documentations or manuals are also appreciated. Thanks in advance!

MaVe
  • 169
  • 2
  • 8

1 Answers1

4

You can use recode with unquote splicing (!!!) on a named vector

library(dplyr)

# vector of new IDs
recode_vec <- recode_table$new
# named with old IDs
names(recode_vec) <- recode_table$old

data %>% 
  mutate(id = recode(id, !!!recode_vec))
#     id
# 1  D78
# 2  L30
# 3  F02
# 4  A24
# 5  B46
# 6  T01
# 7  Q38
# 8  S30
# 9  K88
# 10 O04
# 11 P12
# 12 Z33
Ric S
  • 9,073
  • 3
  • 25
  • 51