0

I have:

testA <- tibble(a = "XYZ spqr MN 123456 MN8Ab1234", b = 1)

fixcontents <- function(mydf, mypattern1, mypattern2, mycol) {
  mydf[[mycol]] <- gsub(mypattern1, mypattern2, mydf[[mycol]])
  return(mydf)
}

testB <- testA
# testB <- fixcontents(testA, "([:alpha:]{3})\\s", "AAA", 1) # replacement does not work
# testB <- fixcontents(testA, "([A-Z]{3})\\s", "AAA ", 1)    # replacement works
# testB <- fixcontents(testA, "([:digit:]{6})", "000000", 1) # replacement does not work
# testB <- fixcontents(testA, "(\\d{6})", "000000", 1)       # replacement works
# testB <- fixcontents(testA, "(11\\w{4})", "000000", 1)     # replacement works
testB

The expected replacements are XYZ spqr MN 123456 MN8Ab1234 -> XYZ spqr MN 000000 MN8Ab1234
and XYZ spqr MN 123456 MN8Ab1234 -> AAA spqr MN 123456 MN8Ab1234

Why is R not recognizing [:alpha:] and [:digit:]?

marcel
  • 389
  • 1
  • 8
  • 21

0 Answers0