Open In App

String Manipulation in R

Last Updated : 15 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

String manipulation is a process of handling and analyzing strings. It involves various operations of modification and parsing of strings to use and change its data. R offers a series of in-built functions to manipulate 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:

1. Using paste() function

Any number of strings can be concatenated together using the paste() function. This function takes separator as argument which is used between the individual string elements . It also has 'collapse' argument , which reflects if we wish to print the strings together . By default, the value of collapse is NULL.

R
str <- paste("Learn", "Code")
print (str)

Output:

"Learn Code"

In case no separator is specified the default separator " " is inserted between individual strings. Example: 

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

Output: 

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

Since, the objects to be concatenated are of different lengths, a repetition of the string of smaller length is applied with the other input strings.

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

Output:

"1--5" "2--6" "3--7" "4--8"

Since, both the strings are of the same length, the corresponding elements of both are concatenated, that is the first element of the first string is concatenated with the first element of second-string using the sep '--'.

2. Using cat() function

Different types of strings can be concatenated together using the cat() function in R, where sep specifies the separator between the strings and file name, in case we wish to write the contents onto a file.

R
str <- cat("learn", "code", "tech", sep = ":")
print (str)

Output:

learn:code:techNULL

The output string is printed with separator " : " and NULL value is appended at the end.

Writing Data into a file using cat() function

To write data into a file , we must give the filename in the "file" argument.

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

Output:

1 2 3 4 5

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

Calculating Length of strings

1. Using length() function

The length() function determines the number of strings specified in the function.

R
print (length(c("Learn to", "Code")))

Output:

2

There are two strings specified in the function.

2. Using nchar() function

nchar() counts the number of characters in each of the strings specified as arguments to the function individually.

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

Output: 

5 4

The output indicates the length of the words "Learn" and then "Code" .

Case Conversion of strings

1. Conversion to upper case

All the characters of the strings specified are converted to upper case , using toupper() function.

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

Output :

"LEARN CODE" "HI"

2. Conversion to lower case

All the characters of the strings specified are converted to lower case.

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

Output : 

"learn code" "hi"

3. Using casefold() function

All the characters of the strings specified are converted to either lowercase or uppercase according to the arguments in casefold() function.

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

Output: 

"learn code" "hi"

By default, the strings get converted to lower case. 

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

Output: 

"LEARN CODE" "HI"

Character replacement

Characters can be translated using the chartr() function , where every instance of old character is replaced by the new character in the specified set of strings.

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

Output:

"An honest mAn gAve thAt"

Every instance of 'a' is replaced by 'A'.

R
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 "#" and "s" by "@" (corresponding to the positions in the old string) .

R
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.

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

Output:

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

Working with substrings

substr() or substring() function 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.

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

Output: 

"Lear"

Extracts the first four characters from the string. 

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

Output: 

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

Replaces the third character of every string with % sign. 

R
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 :

Similar Reads