-3

I R imports columns with no colname as ...1 I need to replace this ... with something else

Trying:

str_replace("hi_...","/././.","&")
Bruno
  • 4,109
  • 1
  • 9
  • 27

2 Answers2

4

Seems like you are trying to replace each dot . with &. You need to escape . as \\. and use str_replace_all. Try this,

library(stringr)  
str_replace_all("hi_...","\\.","&")

Output,

[1] "hi_&&&"

Just in case you want to replace all three dots with & (which I barely think you wanted), use this,

str_replace("hi_...","\\.\\.\\.","&")

OR

str_replace("hi_...","\\.+","&")

Another way to achieve same can be using gsub

gsub("\\.", "&", "hi_...")
Pushpesh Kumar Rajwanshi
  • 18,127
  • 2
  • 19
  • 36
2

We can use

library(stringr)
str_replace("hi_...", "[.]{3}", "&")
akrun
  • 874,273
  • 37
  • 540
  • 662