stringr:: replace all "." to "-" in character vector


My issue is that I should replace all "." characters in a vector to "-", but I really cant find a working function for that. I tried str_replace and str_replace_all, but failed many times.

The example is:
x<-c("a.b.c", "d.e f")
Should return
a-b-c, d-e f

Thanks for your help in advance,

The argument of str_replace_all is a regular expression.

Here's a way how to do it:

x <- c("a.b.c", "d.e f")

# The pattern is always a "regexp" (regular expression)
str_replace_all(x, pattern = "[.]", replacement = "-")
#> [1] "a-b-c" "d-e f"
# or if fixed, then use the  stringr::fixed function
str_replace_all(x, pattern = fixed("."), replacement = "-")
#> [1] "a-b-c" "d-e f"

Created on 2020-09-25 by the reprex package (v0.3.0)

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.