The sub() and gsub() function in R

The sub() and gsub() function in R

The sub() and gsub() function in R is used for substitution as well as replacement operations. But you can use them wisely. Let’s move forward and explore these functions using relevant illustrations.

You can replace the string or the characters in a vector or a data frame using the sub() and gsub() function in R.

Hello folks, we are going to focus on the most useful and beneficial functions in R, i.e. sub() and gsub() functions.

The sub() and gsub() functions in R, will replace the string with a specific string. You can even use regular expressions with the gsub() function. Col right?

Let’s move forward and explore these functions using relevant illustrations.


Syntax of sub() and gsub()

*sub() and gsub(): *The functions which are exclusively useful for string substitution operations in R. You can replace the string in a vector or a data frame and can substitute the specified string.

sub``(pattern, replacement, x)

gsub``(pattern, replacement, x)

Where,

  • Pattern = The pattern or the string which you want to be replaced.
  • Replacement = A input string to replace the pattern string.
  • X = A vector or a data frame to replace the strings.

The sub() function in R

The sub() function in R is used to replace the string in a vector or a data frame with the input or the specified string.

When you are dealing with large data sets, it’s impossible to look at each line to find and replace the target words or strings.

In this case, the sub() function will replace string.

But, the disadvantage of the sub() function is that the function replaces only the first occurrence by leaving all other similarities.

Complicated? Don’t worry. Let’s illustrate this using a simple example.

r programming function

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Functional Programming

Functional Programming: Functional Programming is a Declarative style of Programming Paradigm for writing computer programs.

Are functions from programming really functions?

Are functions from programming really functions? If you are reading this, then most probably you already know quite well what functions are in programming.

How to use strsplit() function in R? - JournalDev - R Programming

strsplit() function in R - strsplit() is an exceptional R function, which splits the input string vector into sub-strings. Now, let’s see how we can split a string vector using the strsplit().

Quantile() function in R - A brief guide - JournalDev R Programming %

You can easliy generate the quantiles in R using the quantile() function. A qunatile is nothing but a sample which is divided into equal groups or sizes.

cbind() function in R programming

You can use cbind() function in R exclusively to bind or combine the multiple columns together. Hello folks, today, our focus will be on the applications