2

I need to anonymize names but in a very specific way so that the format of the entire string is still the same (spaces, hyphens, periods are preserved) but all the letters are scrambled. I want to consistently replace say all A's with C's, all D's with Z's, and so on. How would I do that?

SBala
  • 85
  • 3

2 Answers2

6

We can use chartr

chartr('AD', 'CZ', str1)
#[1] "CZ,ZC. C"

data

str1 <- c('AD,DA. C')
akrun
  • 874,273
  • 37
  • 540
  • 662
2

Maybe use gsub?

    string <- "ABCDEFG"
    text <- gsub('A', 'C', string )
    string <- gsub('D', 'Z', string )

    string 
    [1] "CBCZEFG"
William
  • 166
  • 10