How to Apply the strsplit() Function in R (2 Examples)

In this article, I’ll explain how to apply the strsplit() function in the R programming language.

Creation of Example Data

my_char <- "hey this is my character string"    # Example character string
my_char                                         # Returning character string to console
# "hey this is my character string"

Example 1: Return List with Separated Character String Using strsplit() Function

strsplit(my_char,                               # Basic application of strsplit function
         split = " ")
# [[1]]
# [1] "hey"       "this"      "is"        "my"        "character" "string"

Example 2: Return Vector with Separated Character String Using strsplit() & unlist() Functions

unlist(strsplit(my_char,                        # Apply strsplit & unlist functions
                split = " "))
# [1] "hey"       "this"      "is"        "my"        "character" "string"

Leave a Reply

Your email address will not be published.

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