Find Position of Character in String in R (3 Examples) | Identify Location

 

In this R tutorial you’ll learn how to identify the location of a character in a string.

The article consists of these content blocks:

Let’s dig in:

 

Creation of Exemplifying Data

Consider the following example data:

x <- "aaaaaaaaaaaaaaBaaaaaaaaaBaaaaakjdsnfjlksdf"  # Example string
x                                                  # Print character string to console
# "aaaaaaaaaaaaaaBaaaaaaaaaBaaaaakjdsnfjlksdf"

The previous output of the RStudio console shows that our example data is a character string containing a random sequence of characters.

 

Example 1: Finding Position of Character in String Using gregexpr Function

In Example 1, I’ll explain how to locate a character in a string using the gregexpr command. Within the gregexpr function, we have to specify the character we are looking for (i.e. “B”) and the data we want to search in (i.e. x):

gregexpr("B", x)                                   # Apply gregexpr
# [[1]]
# [1] 15 25
# attr(,"match.length")
# [1] 1 1
# attr(,"index.type")
# [1] "chars"
# attr(,"useBytes")
# [1] TRUE

As you can see, the output of the gregexpr function is relatively difficult to read. However, we can simplify the output by wrapping the unlist function around our previous R code:

unlist(gregexpr("B", x))                           # Apply gregexpr & unlist
# 15 25

Based on the previous output of the RStudio console we can see that the character “B” is located at the positions 15 and 25 within our character string x.

 

Example 2: Finding Position of Character in String Using strsplit & which Functions

The R programming language provides many alternative codes for the location of characters within strings. This Example illustrates how to use the strsplit and which functions for this task. Have a look at the following R code:

which(strsplit(x, "")[[1]] == "B")                 # Apply strsplit & which
# 15 25

The output is exactly the same as in Example 1.

 

Example 3: Finding Position of Character in String Using stringr Package

The R programming language also provides add-on packages that are capable of finding characters in a string.

This Example shows how to use the stringr package to locate characters (or a character pattern). We first have to install and load the stringr package, if we want to use the functions that are contained in the package:

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

Now, we can use the str_locate_all function provided by the stringr package:

str_locate_all(pattern = "B", x)
# [[1]]
#      start end
# [1,]    15  15
# [2,]    25  25

The output is a bit different compared to the previous examples. This time, the output is a list showing the starting and finishing point of our character pattern. Since our character pattern is a single letter of the alphabet (i.e. “B”), the start and end points are identical.

 

Video & Further Resources

In case you need more info on the topics of this article, you may have a look at the following video of my YouTube channel. I show the topics of this article in the video:

 

 

Furthermore, you may want to read the other articles of my website:

 

In this R programming tutorial you learned how to find the position of a character string. Tell me about it in the comments section below, if you have further comments or 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.


2 Comments. Leave new

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