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 <- "w3wiki" String_2 <- "w3wiki" # 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.
Program to Compare two Strings in R
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.