r语言修改数据框中某一变量的值 R:更改数据框中的所有变量

来源:佚名

如何解决R:更改数据框中的所有变量?

我有这种类型的od数据框

数据

structure(list(ID = 1:3,WEEK1 = c(1L,2L,1L),WEEK2 = c(2L,3L),WEEK3 = c(3L,3L,2L)),class = "data.frame",row.names = c(NA,-3L))

DF

ID  WEEK1    WEEK2   WEEK3
1   1        2       3
2   2        2       3
3   1        3       2

我想这样更改变量:

DF

ID  WEEK1    WEEK2   WEEK3
1   A        B       C
2   B        B       C
3   A        C       B

我尝试过

DF %>% mutate_all (1="A",2="B",3="C")

解决方法

df %>%
  mutate(across(matches("WEEK"),chartr,old = '123',new =  'ABC'))
您的足迹: