Open In App

String Manipulation in R

String manipulation basically refers to the process of handling and analyzing strings. It involves various operations concerned with modification and parsing of strings to use and change its data. R offers a series of in-built functions to manipulate the contents of a string. In this article, we will study different functions concerned with the manipulation of strings in R.

Concatenation of Strings

String Concatenation is the technique of combining two strings. String Concatenation can be done using many ways:



paste(..., sep=" ", collapse = NULL)




# R program for String concatenation
 
# Concatenation using paste() function
str <- paste("Learn", "Code")
print (str)

 "Learn Code"




str <- paste(c(1:3), "4", sep = ":")
print (str)

"1:4" "2:4" "3:4"




str <- paste(c(1:4), c(5:8), sep = "--")
print (str)

"1--5" "2--6" "3--7" "4--8"
cat(..., sep=" ", file)




# R program for string concatenation
 
# Concatenation using cat() function
str <- cat("learn", "code", "tech", sep = ":")
print (str)

learn:code:techNULL




cat(c(1:5), file ='sample.txt')

1 2 3 4 5

The output is written to a text file sample.txt in the same working directory.



Calculating Length of strings




# R program to calculate length
 
print (length(c("Learn to", "Code")))

2




print (nchar(c("Learn", "Code")))

5 4

Case Conversion of strings




print (toupper(c("Learn Code", "hI")))

"LEARN CODE" "HI"




print (tolower(c("Learn Code", "hI")))

"learn code" "hi"




print (casefold(c("Learn Code", "hI")))

"learn code" "hi"




print (casefold(c("Learn Code", "hI"), upper = TRUE))

"LEARN CODE" "HI"

Character replacement

Characters can be translated using the chartr(oldchar, newchar, …) function in R, where every instance of old character is replaced by the new character in the specified set of strings. Example 1: 




chartr("a", "A", "An honest man gave that")

Output:

"An honest mAn gAve thAt"

Every instance of ‘a’ is replaced by ‘A’. Example 2: 




chartr("is", "#@", c("This is it", "It is great"))

Output: 

"Th#@ #@ #t"  "It #@ great"

Every instance of old string is replaced by new specified string. “i” is replaced by “#” by “s” by “@”, that is the corresponding positions of old string is replaced by new string. Example 3: 




chartr("ate", "#@", "I hate ate")

Output:

Error in chartr("ate", "#@", "I hate ate") : 'old' is longer than 'new'
         Execution halted 

The length of the old string should be less than the new string.

Splitting the string

A string can be split into corresponding individual strings using ” ” the default separator. Example: 




strsplit("Learn Code Teach !", " ")

Output:

[1] "Learn" "Code"  "Teach" "!"

Working with substrings

substr(…, start, end) or substring(…, start, end) function in R extracts substrings out of a string beginning with the start index and ending with the end index. It also replaces the specified substring with a new set of characters. Example: 




substr("Learn Code Tech", 1, 4)

Output: 

"Lear"

Extracts the first four characters from the string. 




str & lt
- c(& quot
     program&quot, & quot
     with&quot
     , & quot
     new&quot
     , & quot
     language&quot
     )
substr(str, 3, 3) & lt
- & quot
% & quot
print(str)

Output: 

"pr%gram"  "wi%h"     "ne%"      "la%guage"

Replaces the third character of every string with % sign. 




str <- c("program", "with", "new", "language")
substr(str, 3, 3) <- c("%", "@")
print(str)

Output: 

"pr%gram"  "wi@h"     "ne%"      "la@guage"

Replaces the third character of each string alternatively with the specified symbols.


Article Tags :