Strings are generally a one-dimensional (1D) arrays that contain single or multiple values in it. Strings can include character data, numerical data, and any special characters as well.
Strings in R – A brief introduction
A string is nothing but a value which is enclosed by double quotes “” in R. It can be a single value or a multiple. Meanwhile, the empty strings are represented by ” “. You should represent the strings in double quotes ” ” and can use single quotes in between the string as well.
A simple illustration of the string is given below.
#A simple string in R df<-"journal dev - R tutorials"
Note: You cannot use double quotes inside the double-quoted and single quotes inside single-quoted strings.
String construction rules
There are some general rules are there to construct a string.
- The beginning and end quotes should be the same
- You can use single quotes in between double-quotes
- You can use double quotes in between single quotes
- You cannot use single quotes in between single quotes
- You cannot use double quotes in between double-quotes
Valid strings in R
Valid strings are the strings which followed and constructed based on the string rules. Here are some examples of valid strings are shown below.
x<- "ANOVA in R"
Output = “ANOVA in R”
Output = “www.journaldev.com”
df<- "R tutorials 'R programming'in JD"
Output = “R tutorials ‘R programming’in JD”
df<-'string "function" in R'
Output = “string \”function\” in R”
Invalid strings in R
If any string violates the rules then it will become an invalid string. Some of the invalid strings are listed below.
#quotes mix up x<- "ANOVA in R'
Error: unexpected symbol in:
#use of double quotes inside double quotes y<-"R programming "tutorials" is here"
Output = Error: unexpected symbol in “y<-“R programming “tutorials”
As you can see the above examples, you will get to know about the errors and invalid strings as well.
String manipulation in R
In this section, we are going to manipulate the strings in R in various aspects as shown below.
- paste() – To concatenate the strings.
- format() – To format the numerical values.
- nchar() – To count the characters in the string.
- substring() – To extract the specific characters from the string.
We will see how these above mentioned functions manipulate the strings.
paste() function to concatenate strings.
Using paste() function, you can easily concatenate or combine the strings. The paste() function is capable of taking multiple elements as inputs and concatenates them into a string.
To learn more about paste() function in R: paste() in R
Let’s see how this works.
#creates a string w<-'welcome' x<-'to' y<-'Journal dev' z<-'R programming tutorials' #concatenates the strings in to a single string paste(w,x,y,z,sep = '_')
Output = “welcome_to_Journal dev_R programming tutorials”
String formatting using format() function in R
In R you can format the numbers in various aspects as shown below. Let’s see how format() function will work.
#formats the number count df<-format(23.45788,digits = 5)
Output = “23.458”
#formats the scientific values df<-format(c(23,67.890),scientific = T)
Output = “2.300e+01” “6.789e+01”
#formats the decimal values format(34.8,nsmall = 5)
Output = “34.80000”
#formats the number space format(34.8,width=10)
Output = ” 34.8″
#formats the number alignment to left format("JD",width = 10,justify = 'l')
Output = “JD “
#formats the number alignment to right format("JD",width = 10,justify = 'r')
Output = ” JD”
##formats the number alignment to centre format("JD",width = 10,justify = 'c')
Output = ” JD “
Count the number of characters in a string using nchar() function
R has the function named nchar() which counts the number of characters present in a given string as well. Let’s see how it works.
Let me show you how you can count the number characters present in a string using nchar().
nchar("R programming tutorials")
Output = 23
nchar("R is a statistical analysis language")
Output = 36
Change the case of the string using toupper() and tolower() functions
In R you can easily change the case of the string from upper to lower or vice-versa using the tolower() and toupper() functions.
The syntax is given below,
Where, x = input string
toupper("R is a statistical analysis language")
Output = “R IS A STATISTICAL ANALYSIS LANGUAGE”
tolower("R IS A STATISTICAL ANALYSIS LANGUAGE")
Output = “r is a statistical analysis language”
The substring() function in R
The substring() function in R is used to extract the data or the characters from a string. The below illustrations will define the working of substring() in R.
#extractes the specific charater range from the string substring("journaldev",8,11)
Output = “dev”
In the substring() function, the first number and second number indicates the beginning and end of the index number which you want to extract from the string as shown above.
#extractes the specific charater range from the string substring("journaldev",1,7)
Output = “journal”
Note: The index number starts from 1.
Being a fantastic statistical language, R offers various functions function for data manipulation. In this tutorial, we have focussed on the string and its operations in R.
You can use paste(), format(), nchar() and substring() functions to manipulate the strings as discussed in the above sections.
Please be aware of the rules of string construction whenever you construct a string. That’s all about string and its operations in R. Happy learning!!!
More study: R documentation