ADVERTISEMENT
ADVERTISEMENT

Strings in R Language

R Language | Strings: In this tutorial, we are going to learn about the strings, manipulating the strings, strings formatting, strings functions, in R programming language.
Submitted by Bhavya Sri Khandrika, on May 05, 2020

Strings

The strings are defined as a group of characters. In the R language, the strings are commonly written in between either single or double-quotes.

The programmer who works with R needs to get acquainted with the rules while declaring and working with the concept of the strings. Therefore the below is the short description of the rules that are needed to be followed while using strings in the R language. One must make sure that strings are surrounded either with single quotes or double quotes on both sides. The string which starts with the single quotes can have double quotes in the middle of them and vice versa.

Examples for the valid strings:

str1 <- 'String that starts with single quote and ends with single quote too'
print(str1)

str2 <- "String that starts with double quotes and ends with double quotes too"
print(str2)

str3 <- "String that has single quote marks ' inside double quotes"
print(str3)

str4 <- 'String that has Double quotes marks " inside single quote'
print(str4)

Output

[1] "String that starts with single quote and ends with single quote too"
[1] "String that starts with double quotes and ends with double quotes too"
[1] "String that has single quote marks ' inside double quotes"
[1] "String that has Double quotes marks \" inside single quote"

Now as we all know that when we have one part for anything then we always have the counterpart for the same thing and failing to do this will be treated as an invalid string. The following examples help the programmers in acknowledging the difference between the valid and invalid string syntax in the R language.

# The str1's string will not end until the second single quote is found...

str1 <- 'String that starts with single quote and ends with double quotes"
print(str1)

str2 <- 'string that has a Single quote marks ' inside single quote'
print(str2)

str3 <- "Double quotes marks " inside double quotes"
print(str3)

Output

Error: unexpected symbol in:
"
str2 <- 'string"
Execution halted

Explanation:

All the initializations were done in the wrong way that does not match the quotes properly. This Throws error and code does not execute properly.

String Manipulation

Concatenating strings using paste() function

In the R language, the programmer can combine two strings using the paste() function. The paste() function will support any number of arguments into it for the combining purpose.

Syntax:

    paste(..., sep = "", collapse ="")

Parameters:

  • ... : This exemplifies any emblems of arguments that can be integrated using the paste() function
  • sep : the separator between the arguments considered. However, the usage of a separate is always optional and it mainly depends on the user who works with this particular syntax.
  • collapse : the keyword used for the purpose of elimination of the spaces that prevail between the considered number of strings. One should clearly note that space whatever is mentioned here is between the strings but not amid the two words of a string. The usage of collapse is also optional.

Given below is the example code for the demonstration of the above topic:

part1 <- "Welcome"
part2 <- 'to'
part3 <- "includehelp.com"

str1 <- paste(part1, part2, part3)
print(str1)

str1 <- paste(part1, part2, part3, sep = "", collapse = "")
print(str1)

str1 <- paste(part1, part2, part3, sep = "-")
print(str1)

Output

[1] "Welcome to includehelp.com"
[1] "Welcometoincludehelp.com"
[1] "Welcome-to-includehelp.com"

Formatting numbers and strings using format() function

In the R, the numbers and strings can be formatted into the specific style with the help of format() function.

Syntax:

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

Parameters:

  • x is the vector input which depends on the user input.
  • digits is the total number of the digits that will be displayed.
  • nsmall, this is the parameter which is nothing but the minimum number of digits after the decimal point to the right side.
  • scientific is generally set to TRUE with the main intention to display the scientific notations.
  • width demonstrates the minimum number of characters or numbers to be displayed by padding the blanks in the initial stages.
  • justify is the display option that displays the string on left, center or in the right positions of the screen.

Example 1:

# We are formatting the total number of digits to be 
# considered as less than the total number of digits 
# in the value...

value <- 2345.12541132
result <- format(value, digits = 8)
print(result)

Output

[1] "2345.1254"

Example 2:

# We will format the number to its scientific notation...

number <- 87106.912310
result <- format(number, scientific = TRUE)
print(result)

Output

[1] "8.710691e+04"

Example 3:

# Using nsmall we will set the minimum number 
# of digits after the decimal point.

number <- 98.831
result <- format(number, nsmall = 7)
print(result)

Output

[1] "98.8310000"

Example 4:

# Width added some blank spaces to the number...

number <- 89.1223
result <- format(number, width = 12)
print(result)

Output

[1] "     89.1223"

Example 5:

# The justify along with width is used to add the black spaces 
# in the given direction = left(end of string) and 
# center(middle of the string)...

str = "includeHelp.com"

formatedString <- format(str , width = 34, justify = "l")
print(formatedString )
formatedString <- format(str , width = 34, justify = "c")
print(formatedString)

Output

[1] "includeHelp.com                   "
[1] "         includeHelp.com          "

Changing the case with the help of the toupper() and the tolower()

The toupper() and tolower() functions generally contribute to the program when there is a grave need of changing the case of the considered string. In that case, the programmers make use of these basic functions to make the code more comfortable for a user who uses it.

Syntax:

    toupper(x)
    tolower(x)

Parameters:

  • x is considered vector input.

Example:

str <- "Welcome to Includehelp.com"

# We will convert the string to uppercase 
# using toupper() method...
upperCase <- toupper(str)
print(upperCase)

# We will convert the string to lowercase 
# using tolower() method...
lowerCase <- tolower(str)
print(lowerCase)

Output

[1] "WELCOME TO INCLUDEHELP.COM"
[1] "welcome to includehelp.com"

Counting the number of characters in a string using the nchar() function

nchar() function calculates the total number of characters encompassing spaces in the string considered for the purpose of counting the strings.

Syntax:

    nchar(x)

x: the parameter which is used in the above syntax is nothing but a vector output.

Example:

# We will count the number of characters using nchar...

str = "Welcome to Includehelp.com"
print(str)
charCount <- nchar(str)
print(charCount)

Output

[1] "Welcome to Includehelp.com"
[1] 26

Extracting the parts from the considered string using the substring() attribute

The main purpose of making use of substring() function in the R is that it extracts a particular part of the string.

Syntax:

    Substring(x, first, last);

Parameters:

  • x is nothing but the string considered itself.
  • first is the parameter that points to the position of the first character that needs to be extracted.
  • last is the position that corresponds to the last character that is to be extracted from the considered string.

Example:

# We will extract substring.

string <- "Learn programming at includehelp.com"
result <- substring(string, 22, 36)
print(result)

Output

[1] "includehelp.com"
ADVERTISEMENT




ADVERTISEMENT



Comments and Discussions!

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.