Reputation: 79
I am working on a character variable which I want to change the dots "." in it to underscore "". But when I enter the code, it changes all of the character to "______". I tried changing "A"s to "E"s which worked but I could not make it work for dots and underscore.
x <- "TP.MK.CUM.YTL"
y <- str_replace_all(x, ".", "_")
y
# [1] "____________"
Upvotes: 1
Views: 666
Reputation: 887088
Wrap with fixed
as .
is special character to match any character
library(stringr)
str_replace_all(x, fixed("."), "_")
[1] "TP_MK_CUM_YTL"
Or use chartr
in base R
chartr(".", "_", x)
[1] "TP_MK_CUM_YTL"
Upvotes: 1