Remove Numbers from Character String in R (Example)

 

In this tutorial, I’ll illustrate how to extract only the letters from an alphanumeric character string in R programming.

Table of contents:

Let’s take a look at some R codes in action…

 

Example Data

We’ll use the following data as a basis for this R tutorial.

my_alphanum <- "aa11b2c33"                        # Create alphanumeric character string
my_alphanum                                       # Print string
# [1] "aa11b2c33"

The previously shown output of the RStudio console shows the structure of our exemplifying data – A single character string containing numbers and letters.

 

Example: Remove Numbers from Alphanumeric Character String Using gsub() Function

This example shows how to select only the letters from a character string.

To accomplish this, we can apply the gsub function as shown in the following R code:

my_alpha <- gsub("[[:digit:]]", "", my_alphanum)  # Extract letters
my_alpha                                          # Print letters
# [1] "aabc"

The previous syntax has returned the character string “aabc”, i.e. only the letters of our input character string.

 

Video, Further Resources & Summary

In case you need more information on the R code of this page, I recommend watching the following video that I have published on my YouTube channel. In the video, I demonstrate the examples of this article.

 

The YouTube video will be added soon.

 

In addition, you might read the other tutorials on https://www.statisticsglobe.com/.

 

This tutorial has illustrated how to keep only the letters of a character string in R programming. Tell me about it in the comments section below, in case you have any additional 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