0

I would like to replace only a part of a set in R.

Specifically I need to replace the values of a column variable; Let say ds has col1 which takes values like A, B, C, D, E which I want to replace with some different values like 1,15, L1100, PQ7, 243 (some random distinct things).

For example, I want to replace few values not all eg. replace only A, B, C by '1','15', 'L1100' and D, E will remain as it is. Most importantly, I don't want to merge to get this done. I want without merging solution like in python replace function which is very elegant.

ds['col1'].replace(['A','B', 'C', 'D', 'E'],['1','15', 'L1100', 'PQ7', '243'],inplace=True)
Giulio Caccin
  • 2,962
  • 6
  • 36
  • 57
Tamal
  • 45
  • 5
  • 1
    Can you share a reproducible example using dput() ? – Karthik S Mar 10 '21 at 06:56
  • 1
    You can try using `recode` or `case_when` in `dplyr`. See this post https://stackoverflow.com/questions/7547597/dictionary-style-replace-multiple-items – Ronak Shah Mar 10 '21 at 06:56
  • 1
    There are even more possibilities this problem can be approached using dplyr. You could use mutate() with recode() or mutate() with ifelse(). – Markus Mar 10 '21 at 10:17

1 Answers1

1

Here is a small example showing how to randomly replace the first 3 values in v1

> v1 <- c("A", "B", "C", "D", "E")

> v2 <- c(1, 15, "L1100", "PQ7", 243)

> replace(v1, 1:3, sample(v2, 3))
[1] "L1100" "15"    "1"     "D"     "E"

Update

v1 <- c("A", "B", "A", "B", "C", "D", "A", "B", "C", "D", "E", "E", "C", "A", "B", "C", "D", "E", "D", "E")

v2 <- c(1, 15, "L1100")

replace(
  v1,
  v1 %in% c("A", "B", "C"),
  v2[na.omit(match(v1, c("A", "B", "C")))]
)

gives

 [1] "1"     "15"    "1"     "15"    "L1100" "D"     "1"     "15"    "L1100"
[10] "D"     "E"     "E"     "L1100" "1"     "15"    "L1100" "D"     "E"
[19] "D"     "E"
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81
  • Your solution does not work since v1 is a very long list/ a column of a dataset. – Tamal Mar 12 '21 at 16:05
  • @Tamal What do you mean `does not work`? Any error? I don't think the length of long list fails the code. – ThomasIsCoding Mar 12 '21 at 16:07
  • It does not give any error but it does not give the right result. List is like this: ("A", "B", "A", "B", "C", "D","A", "B", "C", "D", "E", "E","C","A", "B", "C", "D", "E", "D", "E"........ so on). I want to replace all A, B and C by '1','15', 'L1100'. Please understand position of A, B, C are fixed but length of list is 100,000. – Tamal Mar 12 '21 at 16:26
  • This works fine. Thank you @ThomasIsCoding – Tamal Mar 13 '21 at 00:53