Home > Net >  Replace multiple values over multiple columns of a data frame in R
Replace multiple values over multiple columns of a data frame in R

Time:09-21

I am trying to replace certain numbers with others over multiple columns of a data set.

1=1
2=1
3=2
4=3
5=3

So if I have:


id      age        A1       weight     Var1       A2    
  
3       5          2         50         1         4
7       23         1         67         5         3
9       78         4         90         3         2
12      14         1         17         2         3

I want to replace those numbers in columns A1, Var1, and A2


id      age        A1       weight     Var1       A2    
  
3       5          1         50         1         3
7       23         1         67         3         2
9       78         3         90         2         1
12      14         1         17         1         2



I think I can do something with mutate_across but I'm not sure

CodePudding user response:

We may use a named vector to match and replace and coalesce with the original column (in case there are other values which we don't need to replace)

library(dplyr)
nm1 <- setNames(c(1, 1, 2, 3, 3), 1:5)
df1 <- df1 %>%
    mutate(across(c(A1, Var1, A2), ~ coalesce(nm1[as.character(.x)], .x)))

-output

> df1
  id age A1 weight Var1 A2
1  3   5  1     50    1  3
2  7  23  1     67    3  2
3  9  78  3     90    2  1
4 12  14  1     17    1  2

data

df1 <- structure(list(id = c(3L, 7L, 9L, 12L), age = c(5L, 23L, 78L, 
14L), A1 = c(2L, 1L, 4L, 1L), weight = c(50L, 67L, 90L, 17L), 
    Var1 = c(1L, 5L, 3L, 2L), A2 = c(4L, 3L, 2L, 3L)),
class = "data.frame", row.names = c(NA, 
-4L))

CodePudding user response:

df1 %>%
   mutate(across(c(A1, Var1, A2), recode, "1" = 1,
                 "2" = 1, `3` = 2, `4` = 3, `5` = 3))

  id age A1 weight Var1 A2
1  3   5  1     50    1  3
2  7  23  1     67    3  2
3  9  78  3     90    2  1
4 12  14  1     17    1  2
  •  Tags:  
  • r
  • Related