Compare Character Strings with Logical Operator Conditionally in R (3 Examples)


This post illustrates how to compare character string vectors using logical operators in R.

The content of the tutorial looks as follows:

Let’s dive right into the programming part!


Construction of Example Data

Let’s first create two exemplifying character string vectors in R.

Our first vector looks like this…

string_1 <- c("Hey", "Yo", "Hello")            # Create first character string
string_1                                       # Print first character string
# [1] "Hey"   "Yo"    "Hello"

…and our second character string vector looks like this:

string_2 <- c("Yay", "Yo", "Yo")               # Create second character string
string_2                                       # Print second character string
# [1] "Yay" "Yo"  "Yo"

Let’s compare these data!


Example 1: Check whether Character Strings Have Common Elements & Return Logical Vector

This example illustrates how to test whether two character string vectors share certain elements by returning a logical vector (i.e. TRUE or FALSE).

For this, we can use the %in% operator as shown below:

string_1 %in% string_2                         # Return logical indicator

The previous output of the RStudio console tells us that the second element of the first character string vector exists in the second vector as well. The other vector elements are unique.


Example 2: Return Common Elements in Two Character String Vectors

In this example, I’ll show how to identify which vector elements do exist in both vectors.

Have a look at the following R code:

string_1[string_1 %in% string_2]               # Identify common elements
# [1] "Yo"

The RStudio console returns the character string “Yo”, i.e. this character string appears in both vectors.


Example 3: Return Value Depending on String Comparison

In Example 3, I’ll explain how to return a certain value depending on a logical condition that takes our two character string vectors into account.

More precisely, the following R code returns “No” in case an element of the first vector does not exist in the second vector, and “Yes” in case an element exists in both vectors.

ifelse(string_1 %in% string_2, "Yes", "No")    # Return values conditionally
# [1] "No"  "Yes" "No"


Video, Further Resources & Summary

Some time ago I have published a video on my YouTube channel, which shows the R code of this tutorial. You can find the video below.


The YouTube video will be added soon.


In addition, you may want to read some of the related articles of my website. I have released numerous articles about related topics such as dplyr and extracting data:


In summary: In this R tutorial you have learned how to check for differences in character strings using logical conditions. If you have any further questions, please let me know in the comments section. Furthermore, don’t forget to subscribe to my email newsletter for updates on new articles.


Subscribe to my free statistics newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.