A Step-by-Step Guide to Strings in R with Examples

What are Strings in R?

The string is any value written either in a single quote or double quote. Internally R takes single quotes as double quotes only.

Valid Strings in R

The quotes at the beginning and end of a string should be both single or double quotes and cannot be mixed.

Example:

x <- "This is a valid proper ' string"
print(x)
y <- 'this is still valid as this one" double quote is used inside single quotes"
print(y)

Output:

This is a valid proper ' string
this is still valid as this single" double quote is used inside single quotes
Lead the Data Science Industry with Confidence
Master Data Science with Us
quiz-icon

Invalid Strings in R

There should not be a single quote in a string which is having double quotes at the beginning and ending and vice versa.

Example:

a<-  'Incorrect string"
print(a)
b <- 'no single quote' should be present within it'
print(b)

Output:

...: unexpected INCOMPLETE_STRING
.... unexpected symbol
1: b <- 'no single quote' should

String Manipulation in R

Concatenating Strings: paste() function

paste() function combines strings and can take many numbers of arguments.

Syntax

The Basic syntax for paste function is :

paste(x,y,z,sep=” “, collapse=NULL)

  • x, y, z represents any number of arguments
  • sep represents any kind of separator between arguments.
  • collapse represents the elimination of space in between strings and not in between two or more words of the same string.

Example:

x <- "Welcome"
y<- 'to'
z <- "Intellipaat Services"
print(paste(x, y, z))
print(paste(x,y,z,sep = "_"))
print(paste(x,y,z, sep="", collapse=""))

Output:

[1] Welcome to Intellipaat Services
[1]  Welcome_to_Intellipaat Services
[1] WelcometoIntellipaat Services

Get 100% Hike!

Master Most in Demand Skills Now!

Formatting numbers and strings in R

Using format() function

Numbers and strings with the help of format() function can be formatted to a specific style.

format(x, digits, nsmall, width, scientific, justify=c(“left”, “centre”, “right”, “none”))

x is a vector, digits are the total number of digits, nsmall is the minimum digits towards right of the decimal point, scientific has TRUE and FALSE to display scientific notation, width is a number of blank spaces at the beginning of a string and justify is to display string towards left, centre or right.

Example:

#illustrating use of digit
dig<- format( 12.3456789, digits = 8)
print(dig)
#illustrating scientific notation
ans <- format(c(5, 13.14521), scientific = TRUE)
print(ans)
#Illustrating justify use of strings
sol <-format"(21.9", width = 6, justify=l)
print(sol)

Output:

[1] "12.345679"
[1] "5.000000e+00"  "1.314521e+01"
[1] "21.9   "

Counting characters in a string – nchar() function

The function counts the number of blank spaces and characters in a string.

  • nchar(a); where “a” is the vector input

Example:

s <- nchar("calculating number of charactersis upper case conversion")")
print(s)

Output:

[1] 32

Changing the case – toupper() & tolower() functions

This function can change the case of letters of a string.

Example:

#converting to upper case
ans <- toupper("This is upper case Transform")
print(ans)

Output:

[1] "THIS IS UPPER CASE TRANSFORM"
Begin your journey with this free Data Science course.
Get Certified in Data Science for Free
quiz-icon

Extracting components of a string via substring() function

Syntax:  substring(a, first, last)

  • where a is vector input of the character
  • first is the position of “a” character to be extracted from starting.
  • last is the position of “a” character to be extracted until the last character

Example:

#Extracting characters from 3rd to 5th position
sol <- substring("Welcome", 3, 5)
print(sol)

Output:

[1] "loc"

Learning how to work with strings in R is important for handling text data. This step-by-step guide will help you understand how to use strings in R easily.

If you want to learn more about data analysis, our Data Science course can teach you all the skills you need to work with different types of data.

Our Data Science Courses Duration and Fees

Program Name
Start Date
Fees
Cohort starts on 4th Feb 2025
₹65,037
Cohort starts on 28th Jan 2025
₹65,037
Cohort starts on 14th Jan 2025
₹65,037

About the Author

Principal Data Scientist

Meet Akash, a Principal Data Scientist with expertise in advanced analytics, machine learning, and AI-driven solutions. With a master’s degree from IIT Kanpur, Aakash combines technical knowledge with industry insights to deliver impactful, scalable models for complex business challenges.