-3

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

Trying:

str_replace("hi_...","/././.","&")
Bruno
  • 3,911
  • 1
  • 8
  • 26

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
  • 17,308
  • 2
  • 17
  • 34
2

We can use

library(stringr)
str_replace("hi_...", "[.]{3}", "&")
akrun
  • 789,025
  • 32
  • 460
  • 575