To remove a specific substring from a string in R, you can use the `gsub` function.
In this example,
str
with a value of "Hello World"
.gsub
function with the pattern "World"
and replacement ""
to remove the specified substring.gsub
function returns a new string with all occurrences of the specified pattern replaced.str
to the console.str <- "Hello World"
str <- gsub("World", "", str)
print(paste("Modified string:", str))
Modified string: Hello
In this example,
text
with a value of "I like apple pie"
.gsub
function with the pattern "apple"
and replacement ""
to remove the specified substring.gsub
function returns a new string with all occurrences of the specified pattern replaced.text
to the console.text <- "I like apple pie"
text <- gsub("apple", "", text)
print(paste("Modified string:", text))
Modified string: I like pie
In this tutorial, we learned How to Remove specific Substring in R language with well detailed examples.