Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing predefined values in R

Tags:

r

recode

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
like image 780
amisos55 Avatar asked Sep 20 '25 12:09

amisos55


2 Answers

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
like image 128
akrun Avatar answered Sep 23 '25 04:09

akrun


An approach using dplyrs rowwise

library(dplyr)

data2 %>% 
  rowwise() %>% 
  mutate(across(v1:v2, ~ data1$cat[data1$id %in% .x])) %>% 
  ungroup()
# A tibble: 3 × 3
  v1    v2    score
  <chr> <chr> <dbl>
1 A     B        15
2 A     C        25
3 B     C        35
like image 31
Andre Wildberg Avatar answered Sep 23 '25 03:09

Andre Wildberg