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.
x <- "This is a valid proper ' string"
y <- 'this is still valid as this one" double quote is used inside single quotes"
This is a valid proper ' string
this is still valid as this single" double quote is used inside single quotes
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.
a<- 'Incorrect string"
b <- 'no single quote' should be present within it'
...: 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.
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.
x <- "Welcome"
z <- "Intellipaat Services"
print(paste(x, y, z))
print(paste(x,y,z,sep = "_"))
print(paste(x,y,z, sep="", collapse=""))
 Welcome to Intellipaat Services
 Welcome_to_Intellipaat Services
 WelcometoIntellipaat Services
Check out the top R Interview Questions to learn what is expected from R professionals!
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.
#illustrating use of digit
dig<- format( 12.3456789, digits = 8)
#illustrating scientific notation
ans <- format(c(5, 13.14521), scientific = TRUE)
#Illustrating justify use of strings
sol <-format"(21.9", width = 6, justify=l)
 "5.000000e+00" "1.314521e+01"
 "21.9 "
Are you interested in learning R Programming from experts? Enroll in our R classroom training in Bangalore now!
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
s <- nchar("calculating number of charactersis upper case conversion")")
Come to Intellipaat’s R Programming Community if you have more queries on R Programming!
Changing the case – toupper() & tolower() functions
This function can change the case of letters of a string.
#converting to upper case
ans <- toupper("This is upper case Transform")
 "THIS IS UPPER CASE TRANSFORM"
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
#Extracting characters from 3rd to 5th position
sol <- substring("Welcome", 3, 5)
Go through our blog on Lists in R!