How to compare strings in R?

In R, the comparison of strings is performed using the “==” operator. This operator will return TRUE if the strings are the same and FALSE if they are not. The “%in%” operator is also available to check if a particular string is present in a vector or list of strings. Additionally, the “match” function can be used to compare two strings and return the position of the first characters that do not match.


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 <- "Mavericks"
string2 <- "mavericks"

#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 <- c("hey", "hello", "HI")
vector2 <- c("hey", "hello", "hi")

#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.

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 <- c("hey", "hello", "greetings")
vector2 <- c("hey", "hello", "hi")

#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:

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

x