Find Positions of Dots in Character String in R (3 Examples)

 

In this R tutorial you’ll learn how to locate all dots (i.e. points) in a character string.

The tutorial contains this:

Let’s dive into it!

 

Creation of Example Data

The following data will be used as basement for this R tutorial:

my_string <- "aaa.b.cccc.d.e"                     # Create example character string
my_string                                         # Print example character string
# [1] "aaa.b.cccc.d.e"

As you can see based on the previous RStudio console output, our example data is a single character string stored in the data object my_string.

 

Example 1: Get All Dot Locations in Character String Using gregexpr() & unlist() Functions

Example 1 illustrates how to locate all dots in a character string.

To accomplish this, we can apply the gregexpr and unlist functions to our character string as shown below:

all_dots <- unlist(gregexpr("\.", my_string))    # Get all dot locations
all_dots                                          # Return all dot locations
# [1]  4  6 11 13

The previous output shows the index positions of all dots in our character string.

 

Example 2: Get Location of First Dot in Character String

In this example, I’ll explain how to extract only the very first dot position in a character string that contains multiple dots.

For this, we can use the data object all_dots that we have created in Example 1 of this tutorial:

first_dot <- all_dots[1]                          # Extract first dot position
first_dot                                         # Print first dot position
# [1] 4

The first dot is located at the fourth character position in our example string.

 

Example 3: Get Location of Last Dot in Character String

Similar to Example 2, we can use the all_dots data object to find the last dot in a character string.

Consider the following R syntax:

last_dot <- all_dots[length(all_dots)]            # Extract last dot position
last_dot                                          # Print last dot position
# [1] 13

The last dot is at the 13th position of our string.

 

Video & Further Resources

Have a look at the following video on my YouTube channel. I explain the contents of this tutorial in the video:

 

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 could have a look at the related tutorials on this website:

 

To summarize: You have learned in this post how to find all positions of dots in a character string in R programming. Let me know in the comments, 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.


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