Replace a substring in a character vector in R

Replace a substring in a character vector in R, will demonstrate how to use the substr and substring functions in this R post.

As the R syntax and output of the two functions are quite similar, I will explain both in the same article.

You will discover when to employ each of the two functions based on the circumstance.

 Replace a substring in a character vector in R

substr(x, start = 2, stop = 5)
substring(x, first = 2, last = 5)

In a character vector, the R substr and substring functions both extract or substitute substrings.

The given example shows the fundamental R syntax for the substr and substring functions.

We will give you three examples of how to use substr and substring in the R programming language in the following tutorial.

Therefore, without further ado, let us begin…

Example 1: Using substr() and substring(), extract the substring from a character vector.

We are going to give you the first example, which is perhaps the most common use of substr and substring: a character vector’s letters are extracted.

Let us start by making such a character vector:

x1 <- "Welcome to finnstats.com"                          

The character object x1 in our sample vector has the short phrase “Welcome to finnstats.com.”

The following is how we can use the R commands substr and substring:

substr(x1, start = 7, stop = 13)                     
[1] "e to fi"
substring(x1, first = 7, last = 13)    

You can see that both functions output “e to fi” to the RStudio console.

How did we accomplish that?

Answer: We specified a starting point (7 in both functions) and a finishing point (13 in both functions), between which we extracted all of the letters from our data object x1.

Please take note that the starting point for substr is called start, the ending point for substring is called last, and the starting point for substr is called stop.

Example 2: Character Vector Moving to the Right from a Specific Position

Let us suppose that after a specific place, we wish to print every character in our character vector. If the stop condition in the substr.

substr(x1, start = 7)                                   
…we get an error:

The desired result is obtained, though, if the final option in the substring is removed:

substring(x1, first = 7)                                
e to finnstats.com

Machine Learning Archives – Data Science Tutorials

How does this function?

The last option in substring has a default value of 1000000L, which explains why. This value is applied if last is not specified.

The remainder of the vector after point 7 is printed because our example vector is less than 1000000L in length.

Example 3: Substring is replaced with substr() and substring()

The replacement of certain characters in a string is another common application of the substr and substring R functions.

We can use both functions to accomplish this once more. Let’s first make two copies of our example vector.

x2a <- x1                                                 
x2b <- x1                                                

…and the let’s use substr()…

substr(x2a, start = 1, stop = 5) <- "finnstats"              
x2a
"finnsme to finnstats.com"
substring(x2b, first = 1) <- " finnstats "                     
x2b
[1] " finnstats finnstats.com"

The replacement must have exactly the same amount of characters as the data it replaces. Check out the gsub function if you wish to replace a substring with a string of a different length.

How to perform Rolling Average in R »

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

one × 3 =