Home > Back-end >  Replace all values ​using indices with case_when
Replace all values ​using indices with case_when

Time:07-23

I have this df:

df_2 <- data.frame(
  x = c("Vanilla", "Grape", "Peach", "Passion Fruit", "Vanilla", 
        "Peach", "Grape", "Watermelon", "Passion Fruit"), 
  y = LETTERS[1:9]
)

I would like to replace the names with values ​​according to indices. The problem occurs when I have only one isolated name (in this case, Watermelon).

Watermelon gets NA, not working the replacement. See:

library(dplyr)
library(magrittr)

df_2 %>% 
  mutate(.data = ., across(.cols = everything(.)[1], .fns = ~ case_when(
    equals(e1 = ., e2 = .[1]) ~ 1, 
    equals(e1 = ., e2 = .[2]) ~ 2, 
    equals(e1 = ., e2 = .[3]) ~ 3, 
    equals(e1 = ., e2 = .[4]) ~ 4, 
    equals(e1 = ., e2 = .[5]) ~ 5
  )))

   x y
1  1 A
2  2 B
3  3 C
4  4 D
5  1 E
6  3 F
7  2 G
###########8 NA H
9  4 I

I added more indexes to see if it worked, like this:

df_2 %>% 
  mutate(.data = ., across(.cols = everything(.)[1], .fns = ~ case_when(
    equals(e1 = ., e2 = .[1]) ~ 1, 
    equals(e1 = ., e2 = .[2]) ~ 2, 
    equals(e1 = ., e2 = .[3]) ~ 3, 
    equals(e1 = ., e2 = .[4]) ~ 4, 
    equals(e1 = ., e2 = .[5]) ~ 5, 
    equals(e1 = ., e2 = .[5]) ~ 5, 
    equals(e1 = ., e2 = .[5]) ~ 5, 
 )))

But it did not work. The NA remains.

How to solve this?

  • I need to stay in the dplyr/ magrittr framework.

CodePudding user response:

transform(df_2, x=match(x,unique(x)))
  x y
1 1 A
2 2 B
3 3 C
4 4 D
5 1 E
6 3 F
7 2 G
8 5 H
9 4 I

transform(df_2, x=as.numeric(factor(x,unique(x))))
  x y
1 1 A
2 2 B
3 3 C
4 4 D
5 1 E
6 3 F
7 2 G
8 5 H
9 4 I
  • Related