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

In this R tutorial you’ll learn how to divide a character string into letters and numbers.

Creation of Example Data

my_string <- "dsjhf2563bjvcsx28736"    # Example string with letters & numbers
my_string
# [1] "dsjhf2563bjvcsx28736"

Example 1: Remove Numbers from Character String

gsub("[[:digit:]]",                    # Exclude numbers
     "",
     my_string)
# [1] "dsjhfbjvcsx"

Example 2: Remove Letters from Character String

as.numeric(gsub("[^0-9.-]",            # Exclude letters
                "",
                my_string))
# [1] 256328736

Related Tutorials & Further Resources

You may find some related R programming language tutorials on topics such as character strings and data elements below.

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.
You need to agree with the terms to proceed

Menu
Top