Site icon R-bloggers

How to replace multiple occurrences of a text within an R string?

[This article was first published on Data Analysis in R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

The post How to replace multiple occurrences of a text within an R string? appeared first on finnstats.

If you are interested to learn more about data science, you can find more articles here finnstats.

How to replace multiple occurrences of a text within an R string?, Do not worry, the R gsub () function is available! This improved sub() function does more than just replace the first occurrence of the target string.

Therefore, gsub in R is your go-to option when you want to completely sanitize a string full of data, removing every instance of the heretical idea.

In R, How to Use gsub ()

The fundamental syntax for gsub in r

gsub(search_term, replacement_term, string_searched, ignore.case = FALSE, perl = FALSE, fixed = FALSE, useBytes = FALSE)

Breaking down the components:

The search term:– a regular expression or a passage of text.

Replacement term:– usually a text fragment

String searched:– must be a string

Ignore case:– allows you to ignore case when searching

Perl:– the ability to use perl regular expressions

Fixed:– option that overrides any other instructions and requires the sub-function to treat the search phrase as a string (helpful when a search string can also be understood as a regular expression).

A working code example:– gsub in r with basic text.

base <- "finnstats.com for data science tutorials and jobs"
gsub("finnstats.com", "finnstats", base)
[1] "finnstats for data science tutorials and jobs"

GSub in R – Regular Expressions

Regular expressions can be used with R’s gsub() function. Below is an illustration of this where we’ve eliminated all punctuation from a phone number.

mobile<-"(206) 6666- 1212"
gsub("[[:punct:][:blank:]]","",mobile)
[1] "20666661212"

As you can see, that phone number shrunk rather quickly! Additionally, it can now be stored and managed much more easily because it fits nicely in a database’s numeric column.

Sub in R – Searching for patterns

Regular expressions can be used to search for more complex patterns. In the example below, we’ll take the initial sequence of 1–3 ns and replace them with a star while sparing any subsequent ns that are greater than that number.

base <- "bnnnnnannannasplit"
gsub("n{1,3}","*",base)
[1] "b**a*a*asplit"

As you can see, it tagged a lot more n’s than the example from our sub tutorial’s original edition did.

Sub in R – Finding Alternative Matches

base <- "All love my finnstats because the website provides tutorials and jobs"
gsub("finnstats|website","finnstats", base)
[1] "All love my finnstats because the finnstats provides tutorials and jobs"

Check your inbox or spam folder to confirm your subscription.

Frequently Asked Data Science Interview Questions »

If you are interested to learn more about data science, you can find more articles here finnstats.

The post How to replace multiple occurrences of a text within an R string? appeared first on finnstats.

To leave a comment for the author, please follow the link and comment on their blog: Data Analysis in R.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.