Search code examples
rrecode

Replacing predefined values in R


I have a defined character values and I need to replace them in another dataset.

Here is the sample dataset

data1 <- data.frame(id = c(1,2,3),
                   cat = c("A","B","C"))
> data1
  id cat
1  1   A
2  2   B
3  3   C

data2 <- data.frame(v1 = c(1,1,2),
                   v2 = c(2,3,3),
                   score = c(15, 25, 35))
> data2
  v1 v2 score
1  1  2    15
2  1  3    25
3  2  3    35

I would like to replace A with 1 for eaxmple, how can I get the desired dataset below?

> data3
  v1 v2 score
1  A  B    15
2  A  C    25
3  B  C    35

Solution

  • Create a named vector from the first data and use that to match and replace the second data columns

    data3 <- data2
    nm1 <- setNames(data1$cat, data1$id)
    data3[1:2] <- lapply(data2[1:2], function(x) nm1[x])
    

    -output

    > data3
      v1 v2 score
    1  A  B    15
    2  A  C    25
    3  B  C    35
    

    Or with dplyr

    library(dplyr)
    data2 %>% 
       mutate(across(v1:v2, ~ tibble::deframe(data1)[.x]))
      v1 v2 score
    1  A  B    15
    2  A  C    25
    3  B  C    35