Test if Character is in String in R (2 Examples)

 

This tutorial illustrates how to identify whether a character is contained in a string in the R programming language.

The content of the article looks as follows:

Let’s dive right into the examples:

 

Creation of Example Data

First, we’ll have to create some example data:

my_x <- "XXXXAXXX"           # Create example string
my_x                         # Print string
# "XXXXAXXX"

Have a look at the previous RStudio console output. It shows that our example string contains a sequence of the alphabetical letter X and in between is an A.

 

Example 1: Check If String Contains Character Using grepl() Function

In Example 1, I’ll show how to test for certain characters in our string using the grepl function. For this, we have to specify the character for which we want to search (i.e. A) and the name of our character string (i.e. my_x):

grepl("A", my_x)             # Apply grepl function
# TRUE

The RStudio console returned the logical value TRUE after running the previous R code. This means that the character A is contained in our string.

Let’s apply the grepl function to a character that is not in our string:

grepl("B", my_x)             # Non-existing character
# FALSE

As you can see, the RStudio console returned FALSE, i.e. the letter B is not in our string.

 

Example 2: Check If String Contains Character Using str_detect() Function of stringr Package

Alternatively to the grepl function shown in Example 1, we can also use the str_detect function provided by the stringr add-on package.

If we want to use the functions of the stringr package, we first have to install and load stringr:

install.packages("stringr")  # Install & load stringr
library("stringr")

Now, we can use str_detect as shown below:

str_detect(my_x, "A")        # Apply str_detect function
# TRUE

The str_detect function also returns a logical value, i.e. TRUE.

 

Video, Further Resources & Summary

In case you need more info on the content of this tutorial, you may have a look at the following video of the Statistics Globe YouTube channel. In the video, I’m explaining the R programming code of this tutorial:

 

Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.

YouTube Content Consent Button Thumbnail

YouTube privacy policy

If you accept this notice, your choice will be saved and the page will refresh.

 

Also, you might want to have a look at some of the related articles on this homepage:

 

This article showed how to check for character patterns in strings in R. In case you have any additional questions, tell me about it in the comments. Furthermore, please subscribe to my email newsletter in order to receive regular updates on new posts.

 

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.

Menu
Top