Open In App

Program to Compare two Strings in R

Last Updated : 06 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

String comparison is a common operation where two strings are compared in a case sensitive manner and check whether they are equal or not. In this article you will learn the various way in which you can compare two string in R language.

Concepts Related to the Topic :

  • String Comparison: Comparison of two strings and check whether they are equal or not.
  • Equality Operators (==): It is used to compare two string for equality and inequality.
  • Case Sensitivity: In R language programming strings are considers as case-sensitive by default. There are functions like tolower() or toupper() to make case-insensitive comparison.
  • tolower(): tolower() is the function to convert the string to lower case.
  • toupper(): toupper() is the function to convert string to upper case.
  • identical(): indentical() is function to compare two string and true or false according to the input string.
  • grepl(): grepl() is function to compare two string partially.

Strings

A collection of character variables make up a string. There is only one dimension to the cast of characters. In R, a string is defined as one or more letters surrounded in a pair of matching single or double quotes. In R programming, strings are used to represent textual material and can include special characters, digits, and spaces. ” is used to denote an empty string. R Strings are always kept in double-quoted form in storage. Single quotes may appear inside of a double-quoted string. Single quotes are not permitted in single-quoted strings. In a similar vein, double quotations cannot be enclosed in double quotes.

Steps Needed :

  • Define the two strings you want to compare.
  • Use a comparison operator (== or !=) to compare the strings.
  • Use functions like tolower() or toupper() for case-insensitive comparisons.
  • Use grepl() for partial comparision.
  • Use identical() for string comparision.
  • Use if condition and print the result.

Case-sensitive comparison

In this example we have compare two string in case sensitive.

R




# Define two strings(str1 and str2)
String_1 <- "geeksforgeeks"
String_2 <- "GEEKSFORGEEKS"
  
# Conditional if statement to check whether string are equal or not
if (String_1 == String_2) {
  cat("The strings are equal")
} else {
  cat("The strings are not equal")
}


Output:

The strings are not equal
  • Since the case are not equal for both the strings, so output is false i.e. “The strings are not equal”.

Case-Insensitive comparison

In this example we have compare two string in case Insensitive.

R




# Define two strings(str1 and str2)
String_1 <- "geeksforgeeks"
String_2 <- "GEEKSFORGEEKS"
  
# tolower() to convert string to lower case
ans <- tolower(String_1) == tolower(String_2)
  
# Conditional if statement to check whether ans is true or not
if (ans) {
  cat("The strings are equal")
} else {
  cat("The strings are not equal")
}


Output:

The strings are equal
  • Since, the case are not equal for both the string so we made both the string in lower case using tolower() function.
  • Output is true i.e. “The strings are equal” as both the string have now become equal.

Compare two strings uising identical()

There is another method to compare string using identical() function. An identical function is used in R to check whether two R objects are equal/ identical or not.

R




# Define two strings(str1 and str2)
String_1 <- "geeksforgeeks"
String_2 <- "GEEKSFORGEEKS"
  
# identical() to compare two string and return true or false
ans <- identical(tolower(String_1),tolower(String_2))
  
# Conditional if statement to check whether ans is true or not
if (ans) {
  cat("The strings are equal")
} else {
  cat("The strings are not equal")
}


Output:

The strings are equal
  • In this example we have made both the string in lower case using tolower() function and passed it in identical() function. The function compares them and output is true i.e. “The strings are equal” since, the string have now become equal.

Compare two strings partially using grepl()

The word “grepl” stands for “grep logical”. The grepl() function in R simply searches for matches in characters or sequences of characters present in a given string.

R




# Define two strings(str1 and str2)
String_1 <- "geeks for geeks"
String_2 <- "for"
  
# grepl() function to check if "for" is present in  or not "geeks for geeks"
ans <- grepl(String_2,String_1)
  
# Conditional if statement to check whether ans is true or not
if (ans) {
  cat("The strings are equal")
} else {
  cat("The strings are not equal")
}


Output:

The strings are equal
  • In this example we check whether “for” is present in “geeks for geeks” string or not.
  • Compare them and print the output if it is present i.e. “The strings are equal” or print false i.e. “The strings are not equal”.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads