Match Wildcard Pattern and Character String in R (Example)

 

On this page, I’ll illustrate how to match wildcard patterns and character strings in the R programming language.

The tutorial will contain these contents:

So now the part you have been waiting for – the example!

 

Constructing Example Data

At the start, let’s construct some example data in R. We can create a wildcard pattern as shown below:

my_wildcard <- "xxx*"                               # Create character string with wildcard
my_wildcard                                         # Print character string with wildcard
# [1] "xxx*"

Next, we can create a vector of character string as shown in the following R code:

my_vector <- c("xxxy", "yyyx", "xxx5", "abc")       # Create vector of character strings
my_vector                                           # Print vector of character strings
# [1] "xxxy" "yyyx" "xxx5" "abc"

As you can see based on the previous output of the RStudio console, our character vector contains four different vector elements.

 

Example: Match Pattern with Wildcard Using grep() & grepl() Functions

The following code shows how to match wildcard patterns and character strings in R.

We can use the grep function to return the positions of matching character strings in our vector as shown below:

grep(my_wildcard, my_vector)                        # Return positions of matching patterns
# [1] 1 3

The grep function can also be used to return the matching pattern in our vector:

grep(my_wildcard, my_vector, value = TRUE)          # Return matching character strings
# [1] "xxxy" "xxx5"

We can apply the grepl function to return a logical indicator whether the elements of our character vector are matching:

grepl(my_wildcard, my_vector)                       # Return logicals for matching patterns
# [1]  TRUE FALSE  TRUE FALSE

And we can also use the grepl function to create a vector or data frame subset with matching patterns:

my_vector_subset <- my_vector[grepl(my_wildcard, my_vector)]  # Create subset of matching elements
my_vector_subset                                    # Print subset of matching elements
# [1] "xxxy" "xxx5"

 

Video, Further Resources & Summary

Some time ago I have published a video on my YouTube channel, which explains the R programming codes of this tutorial. Please find the video below.

 

 

Furthermore, you may have a look at the related tutorials of this homepage. You can find some tutorials below:

 

At this point you should know how to perform matching with globbing patterns in the R programming language. If you have further questions, let me know in the comments section below.

 

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