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
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
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.
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 "
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
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"
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"
Go through our blog on Lists in R!