R Strings
Strings are used for storing text. In R, when you enclose a value in either single or double quotes, it is considered a string. R internally stores all strings within double quotes, even if they are initially created with single quotes.
Rules to write R Strings
Here are the guidelines for creating a valid string in R programming:
- You must use either single or double quotes to encapsulate a string value, like
str1 <- 'Hello'orstr2 <- "Hello". - Since single and double quotes are the special characters denoting an R string, inserting them into a string requires careful handling.
- To incorporate a single quote within a string, enclose it with double quotes, as in
str1 = "Jim's book.". - For including a double quote within a string, use single quotes around it, for example,
str1 = 'Jim says, "Hello World"'. - Single or double quotes can be included at the beginning or end of a string value as they are, such as
str1 = "'Hello".
R String Operations
- R – Assign a String to a Variable
- R – Multiline String
- R – Find length of String
- R – Check a String
- R – Concatenate or Combine Strings
- R – Format Numbers & Strings
- R – Extract String or Substring of a String
- R – Convert String to Upper-case
- R – Convert String to Lower-case
R – Assign a String to a Variable
Assigning a string to a variable is done with the variable followed by the <- or = operator and the string:
Example:
str <- "Hello world"
str1 = "Hello world1"
# print the value of str
str
str1
#output:
[1] "Hello world"
[1] "Hello world1"
R – Multiline String
We can assign a multiline string to a variable.
Example:
str <- "Hello,
I am Jim,
R programming is interesting
I love it very much."
# print the value of str
str
#output:
[1] "Hello,\nI am Jim,\nR programming is interesting\nI love it very much."
R – Find length of String
We first initialize a variable with a String value and then we will find its length using nchar(string) function. When dealing with numbers nchar automatically type casts number to character by using nchar(num).
Example:
str = 'Hello World!'
length.str = nchar(str)
print ("length.str")
print (length.str)
num = 12345
length.num = nchar(num)
print ("length.num")
print (length.num)
#output:
[1] "length.str"
[1] 12
[1] "length.num"
[1] 5
R – Check a String
We will use the grepl() function to check if one or more characters are present in a string:
Example:
str <- "Hello World!"
grepl("H", str)
grepl("World", str)
grepl("Z", str)
#output:
[1] TRUE
[1] TRUE
[1] FALSE
R – Concatenate or Combine Strings
Syntax:
paste(…, sep="", collapse=NULL)
| paste | is the keyword |
| … | input strings separated by comma. |
| sep | is a character that acts as a separator |
| collapse | is an optional character to separate the results |
Example:
str1 <- 'Hello'
str2 <- 'World!'
str3 <- 'I am'
str4 <- 'Jim'
# concatenate two strings
result1 <- paste(str1,str2)
print ("result1")
print (result1)
# concatenate two strings with no separator
result2 <- paste(str1,str2,sep="")
print ("result2")
print (result2)
# concatenate multiple string
result3 = paste(str1,str2,str3,str4,sep="-")
print ("result3")
print (result3)
#output:
[1] "result1"
[1] "Hello World!"
[1] "result2"
[1] "HelloWorld!"
[1] "result3"
[1] "Hello-World!-I am-Jim"
R – Format Numbers & Strings
String formatting in R is done with the sprintf() and format() function.
Example – sprintf():
# Create two variables with values
x <- 50
y <- 2.87901
# Format a string with the two variable values
result <- sprintf("The number is %d, and y is %.2f.", x, y)
# Print the result
print(result)
#output:
[1] "The number is 50, and y is 2.88."
Example – format():
# Total number of digits displayed. Last digit rounded off.
result <- format(11.123456789, digits = 9)
print(result)
# Display numbers in scientific notation.
result <- format(c(5, 11.12531), scientific = TRUE)
print(result)
# The minimum number of digits to the right of the decimal point.
result <- format(21.37, nsmall = 5)
print(result)
# Format treats everything as a string.
result <- format(22)
print(result)
# Numbers are padded with blank in the beginning for width.
result <- format(11.2, width = 5)
print(result)
# Left justify strings.
result <- format("Good", width = 7, justify = "l")
print(result)
# Justfy string with center.
result <- format("Good", width = 7, justify = "c")
print(result)
#output:
[1] "11.1234568"
[1] "5.000000e+00" "1.112531e+01"
[1] "21.37000"
[1] "22"
[1] " 11.2"
[1] "Good "
[1] " Good "
R – Extract String or Substring of a String
Syntax:substring(c, first, last)
Example:
#Get Substring from a String with first and last positions
str <- 'Welcome to R tutorial'
subStr1 <- substring(str, 13, 19)
print (subStr1)
#Get Substring from a String Without last positions
subStr2 <- substring(str, 13)
print (subStr2)
#output:
[1] "R tutor"
[1] "R tutorial"
R – Convert String to Upper-case
To convert Strings to upper-case is done using toupper() function.
Example:
str <- 'Hello World'
result <- toupper(str)
print(result)
#output
[1] "HELLO WORLD"
R – Convert String to Lower-case
To convert Strings to lower-case is done using tolower() function.
Example:
str <- 'HELLO WORLD, good job!'
result <- tolower(str)
print(result)
#output:
[1] "hello world, good job!"
Conclusion
In this R Tutorial, we have learned R Strings Operations.
