I have data as below, I like to replace "." with space using gsub() but I could not get correct output.
data<-c("12.57869486" ,"12.57869582" ,"12.57870155")
a<- gsub("."," ", data)
a
[1] " " " " " "
Many way to achieve this:
1) Use the fixed
parameter of gsub:
From ?gsub
:
fixed logical. If TRUE, pattern is a string to be matched as is. Overrides all conflicting arguments.
So adding fixed=TRUE
to your command is enough to avoid intepreting the . as any character (regex mode):
> a<-gsub(".", " ", data, fixed=TRUE)
> a
[1] "12 57869486" "12 57869582" "12 57870155"
2) Use chartr
(from G. Grothendieck comment):
chartr(".", " ", data)
3) Escape the special char .
which mean any character in regex: (from Tim Biegeleisen comment)
gsub("\\.", " ", data)
Escape with a double backslashgsub("[.]", " ", data)
Escape using character classIn long regex I tend to prefer the second syntax as I find it more readable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With