To replace multiple spaces with a single space in R, you can use the `gsub` function with a regular expression pattern.
In this example,
gsub
function with the regular expression pattern \s+
to replace multiple spaces with a single space in the string str
.str
.str <- "This is a test string"
modifiedStr <- gsub("\s+", " ", str)
print(modifiedStr)
[1] "This is a test string"
In this tutorial, we learned How to Replace multiple Spaces with a Single Space in String in R language with well detailed examples.