Find Elements in String Vector that Contain Certain Character in R (2 Examples)

 

In this post you’ll learn how to check whether a character is contained in a vector of character strings in the R programming language.

The page looks as follows:

Let’s do this:

 

Introduction of Example Data

We’ll use the following data as basement for this R programming tutorial:

x <- c("abc", "ABC", "xxx", "aaa", "yyy")    # Create character vector
x                                            # Print character vector
# [1] "abc" "ABC" "xxx" "aaa" "yyy"

Have a look at the previously shown output of the RStudio console. It visualizes that our example data is a vector of character strings containing five vector elements.

 

Example 1: Extract Elements in String Vector that Contain Certain Character Using grep() Function

In this example, I’ll explain how to return all character string elements in a vector object that contain a particular character.

More precisely, we’ll use the grep function to search for the character “a”.

Within the grep function, we set the value argument to be equal to TRUE to return the actual character strings instead of their index positions.

Let’s do this in R!

x_subset1 <- grep("a", x, value = TRUE)      # Extract elements with match
x_subset1                                    # Print vector subset
# [1] "abc" "aaa"

As you can see, the vector elements “abc” and “aaa” have been returned, since both contain the character “a”. Note that the vector element “ABC” was not returned, since the grep function is case-sensitive.

 

Example 2: Extract Elements in String Vector that Contain Certain Character Using str_detect() Function of stringr Package

In Example 1, we have used the basic installation of the R programming language to match a character and the elements of a character string vector.

Example 2 shows how to use the functions of the stringr package for this task. In order to use the functions of the stringr package, we first have to install and load stringr:

install.packages("stringr")                  # Install stringr package
library("stringr")                           # Load stringr

Now, we can apply the str_detect function of the stringr package as shown below:

x_subset2 <- x[str_detect(x, "a")]           # Extract elements with match
x_subset2                                    # Print vector subset
# [1] "abc" "aaa"

The same output as in Example 1 has been returned after executing the previous R code.

 

Video, Further Resources & Summary

In case you need further information on the R code of this article, I can recommend having a look at the following video on my YouTube channel. In the video, I’m explaining the R code of this article:

 

 

Furthermore, you might have a look at the related R articles on this website. Some tutorials are listed below.

 

In this R tutorial you have learned how to test if a character is contained in an array of character strings. Let me know in the comments, if you have further questions.

 

Subscribe to the Statistics Globe 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.

Top