Split Character String into Letters & Numbers in R (2 Examples)

 

This tutorial shows how to divide a character string into letters and numbers in R.

The content of the article is structured like this:

Let’s get started.

 

Creation of Example Data

The following data is used as a basis for this R programming tutorial:

x <- "123asdf45gh6"                                 # Create example character string
x                                                   # Print example character string
# [1] "123asdf45gh6"

As you can see based on the previous output of the RStudio console, our example data is a single character string that contains different letters and numbers.

 

Example 1: Extract Letters from Character String

In this example, I’ll demonstrate how to remove all numbers from a character string.

For this task, we can apply the gsub function as shown in the following R code:

x_letters <- gsub("[[:digit:]]", "", x)             # Extract letters from character string
x_letters                                           # Print letters
# [1] "asdfgh"

The previous output shows that we have kept only non-numeric values in our data.

We might also split this output into a vector containing only one letter in each vector element by using the strsplit and unlist functions:

x_letters_split <- unlist(strsplit(x_letters, ""))  # Split characters into vector
x_letters_split                                     # Print vector
# [1] "a" "s" "d" "f" "g" "h"

 

Example 2: Extract Numbers from Character String

Example 2 illustrates how to drop all letters from a character string.

Once again, we can use the gsub function for this task:

x_numbers <- gsub("[^0-9.-]", "", x)                # Extract numbers from character string
x_numbers                                           # Print numbers
# [1] "123456"

Similar to Example 1, we can use the strsplit and unlist functions to split our number into digits:

x_numbers_split <- unlist(strsplit(x_numbers, ""))  # Split characters into vector
x_numbers_split                                     # Print character vector
# [1] "1" "2" "3" "4" "5" "6"

Furthermore, we might convert the data class of our numbers from character to numeric using the as.numeric function:

x_numbers_split_num <- as.numeric(x_numbers_split)  # Convert character to numeric
x_numbers_split_num                                 # Print numeric vector
# [1] 1 2 3 4 5 6

 

Video & Further Resources

If you need more explanations on the R programming syntax of this article, you may have a look at the following video which I have published on my YouTube channel. In the video, I illustrate the R programming code of this post.

 

 

Furthermore, you might read the other tutorials on this website. A selection of tutorials is shown below:

 

Summary: In this R programming tutorial, you have learned how to split an alphanumeric character string into letters and numbers. In case you have additional questions, don’t hesitate to tell me about it in the comments section.

 

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