Reputation: 306
how I can refer to any single character in R>? in my case it would be 3 any chars. I bit overwhelmed with its power, so many help around !) In my case I need to replace any 3 characters in pos 25-27 with "XXX". I successfully used this code to replace specific chars, tried to use %,* instead of abc with no avail. Tx much
d <- c("alpha 1 bravo 0 charlie_abc v3")
sub("^(.{24})abc", "\\1XXX", d)
[1] "alpha 1 bravo 0 charlie_XXX v3"
Upvotes: 0
Views: 37
Reputation: 17299
Here are two ways, using pattern "^(.{24}).{3}"
or using substring
:
d <- c("alpha 1 bravo 0 charlie_abc v3")
sub("^(.{24}).{3}", "\\1XXX", d)
#> [1] "alpha 1 bravo 0 charlie_XXX v3"
d <- c("alpha 1 bravo 0 charlie_abc v3")
substring(d, 25, 27) <- "XXX"
d
#> [1] "alpha 1 bravo 0 charlie_XXX v3"
Upvotes: 3