Home > Net >  Replacing predefined values in R
Replacing predefined values in R

Time:01-06

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

CodePudding user response:

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

CodePudding user response:

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

CodePudding user response:

You can use the built-in constant LETTERS to convert the numbers to letters across all relevant columns by using the digits as indices of LETTERS thus subsetting the constant on them:

library(dplyr)
data2 %>%
  mutate(across(starts_with("v"), ~LETTERS[.]))
  v1 v2 score
1  A  B    15
2  A  C    25
3  B  C    35

Data:

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