Why is sub in R not recognizing the pattern that I provided in R?

35 Views Asked by At
mystring <- c("code IS (384333)\n   AND parse = TURE \n ) \n 
              \n code IS (43343344)\n ) some information here
              code IS (23423422) ) and more information")

I'm trying to replace the 2nd to 40th characters of mystring with "HELLO". However, sub is not giving me the correct output:

> sub(pattern = substr(mystring, 2, 40), replacement = "HELLO", mystring)
[1] "code IS (384333)\n   AND parse = TURE \n ) \n \n              \n code IS (43343344)\n ) some information here\n              code IS (23423422) ) and more information"

The correct output is

[1] "cHELLO \n \n              \n code IS (43343344)\n ) some information here\n              code IS (23423422) ) and more information"
0

There are 0 best solutions below