# How to use the Substring Function in R

### How to Utilize substring Function in R (4 Cases)

The substring() action in R can be utilized to remove a substrand in a character vector in a specified position.

This action utilizes the next syntax:
substranf(text, first, last)

where:
•text: Label of the character vector
•first: The first element to be removed
•last: The last element to be removed

Remember that the substr() action does the exact same thing, but with slightly different dispute labels:
substr(text, first, last)

where:
•x: Label of the character vector
•start: The first element to be removed
•stop: The last element to be removed

The cases show how to utilize the substrand() action in practice with the next data grid in R:
#design data frame
df : data.frame(team=c(‘Mavericks’, ‘Hornets’, ‘Rockets’, ‘Grizzlies’))

#view data frame
df

team
1 Mavericks
2 Hornets
3 Rockets
4 Grizzlies

### Case 1: Remove Characters Between a Certain Position

The following code shows how to utilize the substrand() action to remove the characters between positions 2 and 5 of the “team” column:

#design new column that includes characters between positions 2 and 5
df\$between2_5 : substrand(df\$team, first=2, last=5)

#view updated data grid
df

team between2_5
1 Mavericks aver
2 Hornets orne
3 Rockets ocke
4 Grizzlies rizz

Remember that the new column includes the characters between positions 2 and 5 of the “team” column.

### Case 2: Remove First N Characters

The next code shows how to utilize the substrand() action to extract the first 3 characters of the “team” column:’

#design new column that includes first 3 characters
df\$first3 : substrand(df\$team, first=1, last=3)

#view updated data grid
df

team first3
1 Mavericks Mav
2 Hornets Hor
3 Rockets Roc
4 Grizzlies Gri

Notice that the new column includes the first three characters of the “team” section.

### Case 3: Remove Last N Characters

The next code shows how to utilize the substrand() action to extract the last 3 characters of the “team” column:

#design new column that includes last 3 characters
df\$last3 : substrand(df\$team, nchar(df\$team)-3+1, nchar(df\$team))

#view updated data frame
df

team last3
1 Mavericks cks
2 Hornets ets
3 Rockets ets
4 Grizzlies ies

Notice that the new column includes the last three characters of the “team” column.

### Case 4: Replace a Substring in R

The next code shows how to utilize the substrand() action to replace the first 3 characters of the values in the “team” section with 3 asterisks:

#replace first 3 characters with asterisks in team section
substrand(df\$team, first=1, last=3) : “***”

#view updated data grid
df

team
1 ***ericks
2 ***nets
3 ***kets
4 ***zzlies

Substring() action in R is openly utilized to either replace the characters current in the information or design string manipulation. You can simply extract the necessary characters from a character string and likewise the values in a replacement string.

### The substrand() Function Syntax

Substring: We can perform several things like removing of amounts, replacement of amounts and greater. For this we utilize actions like substr() and substrand().

1 substr(x,start,stop)
2 substrand(x,first,end=1000000L)
As:

> x = the input information / file.
> Start / First= beginning index of the substrand.
> Stop / End= Stopping index of the substrand.

Remove characters utilizing substring() function in R

1 #yields the characters
2 from 1,11
3 df:(“Notebook_dev_private_limited”)
substrand(df,1,11)

Result = “Notebook_dev”

1 #yields the characters
2 from 1-7
3 df:(“Notebook_dev”)
substring(df,1,7)

Result = “Notebook”

As observed, the substrand() function in R grasps the start/begin and last/stop amounts as disputes and indexes the given string and yields a necessary substrand of mentioned dimensions in the end position.

### Replace utilizing substrand() function in R

With the aid of substrand() action, you can likewise replace the amounts in the strand with your desired values.

1 #yields the string by exchanging the _ by void
2 df:(“We are_creators”)
3 substrand(df,7,7)=” “
4 df

Output = “We are creators”

1 #string reinstatement
2 df:(“R=is a language created for statistical investigation”)
3 substring(df,2,2)=” “
4 df

Output = “R is a language created for statistical investigation”

In the regular expression, you may replace the amounts in an original string with your desired length value.

For the prior case, you have exchanged the ‘_’ (underscore) plus “=” (equal symbol) with a ” ” (void).

String replacement utilizing substrand() function

If neccessary to replace some amounts, which must reflect in every string present?

Replace the amounts and allowing them to match on every the string current.

1 #exchanges the 4th letter of every string by \$
2df:c(“Moca”, “Paloma”, “Kelly”, “Hayato”, “Joseph”, “Alok”)
3 substring(df,4,4):c(“\$”)
4 df

Output = “Moc\$” “Pal\$ma” “Kel\$y” “Hay\$to” “Jos\$ph” “Alo\$”

Every 4th letter of the strings is exchanged by ‘\$’ sign!.

Well, that is substrand() for you. It can exchange the highlighted positions with the given amount.

In the above sample, every 4th letter in every input string was exchanged by the ‘\$’ symbol by the substrand() action.

The utilization of substr() and str_sub() action in R

We can design a data grid with sample information having 2 sections namely Technologies and Acceptance. Now remove some direct characters from this information.

1 #designs the data grid
2 df:data.frame(Technologies=c
3(“Informationscience”,”machinestudy”,”Deepstudy”,”Artificalknowledge”),Acceptance=c(“70%”,”85%”,”90%”,”95%”))
df

Technologies Acceptance
1 Datascience 70%
2 machinestudy 85%
3 Deepstudy 90%
4 Artificalknowledge 95%

A data frame has been designed. Now remove a few text. To complete, execute the below code to remove characters from 8-10 in every string in Technologies section utilizing substr() action in R.

1 #designs new section with removed amounts
2 df\$Removed_Technologies=substr(df\$Technologies,8,10)
3 df

Result =

Technologies Acceptance Removed_Technologies
1 Informationscience_IS 70%
2 machinestudy_MS 85%
3 Deepstudy_DS 90%
4 Artificalintelligence_AI 95%

It can be seen a new column was designed with extracted data. Now the data can remove by specifying the index values.

The utilize of str_sub() action in R

We saw the substr() action at work. Now, as mentioned prior, we will be looking into the str_sub() action and its way of removal.

Once gain design the exact data grid including the information of Technologies and its popularity as well.

1 df:
2data.frame(Technologies=c(“Datascience”,”machinelearning”,”Deeplearning”,”Artificalknowledge”),Acceptance=c(“70%”,”85%”,”90%”,”95%”))
df

Technologies Acceptance
1 Informationscience 70%
2 machinestudy 85%
3 Deepstudy 90%
4 Artificalknowledge 95%

Take advantage of the str_sub() action, which will give the assigned characters as output. Taking/designing a substrand in R can be completed in numerous ways and this being one of them.

1 #utilizing the str_sub action
2df\$Removed_Automations=str_sub(df\$Technologies,10,15)
3 > df

As shown the str_sub() action removed the indexed values and yields the expression as shown beneath.

Automations Acceptance Removed_Technologies
1 Informationscience 70%
2 machinelearning 85%
3 Deeplearning 90%
4 Artificalintelligence 95%

Scroll to top