Home » How to Compare Strings in R (3 Examples)

How to Compare Strings in R (3 Examples)

by Erma Khan

You can use the following methods to compare strings in R:

Method 1: Compare Two Strings

#case-sensitive comparison
string1 == string2

#case-insensitive comparison
tolower(string1) == tolower(string2)

Method 2: Compare Two Vectors of Strings

#case-sensitive comparison
identical(vector1, vector2)

#case-insensitive comparison
identical(tolower(vector1), tolower(vector2))

Method 3: Find Similarities Between Two Vectors of Strings

#find which strings in vector1 are also in vector2
vector1[vector1 %in% vector2]  

The following examples show how to use each method in practice.

Example 1: Check if Two Vectors Are Identical

The following code shows how to compare two strings in R to determine if they’re equal:

#define two strings
string1 
string2 

#case-sensitive comparison
string1 == string2

[1] FALSE

#case-insensitive comparison
tolower(string1) == tolower(string2)

[1] TRUE

The case-sensitive comparison returns a value of FALSE since the two strings are not perfectly identical.

However, the case-insensitive comparison returns a value of TRUE since the two strings contain the same characters in the same order, regardless of case.

Example 2: Compare Two Vectors of Strings

The following code shows how to use the identical() function to determine if two vectors of strings are equal:

#define two vectors of strings
vector1 #case-sensitive comparison
identical(vector1, vector2)

[1] FALSE

#case-insensitive comparison
identical(tolower(vector1), tolower(vector2))

[1] TRUE

The case-sensitive comparison returns a value of FALSE since the two vectors don’t contain the exact same strings in the same case.

However, the case-insensitive comparison returns a value of TRUE since the two vectors contain the same strings, regardless of case.

Example 3: Find Similarities Between Two Vectors of Strings

The following code shows how to use the %in% operator to find which strings in one vector belong to another vector:

#define two vectors of strings
vector1 

#find which strings in vector1 are also in vector2
vector1[vector1 %in% vector2]

[1] "hey"   "hello"

From the output we can see that the strings “hey” and “hello” exist in both vector1 and vector2.

Related: How to Use %in% Operator in R

Additional Resources

The following tutorials explain how to perform other common operations in R:

How to Compare Two Columns in R
How to Compare Two Vectors in R
How to Find Location of Character in String in R
How to Convert a Vector to String in R

Related Posts