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" |
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" |
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" |
unlist(strsplit(my_char, # Apply strsplit & unlist functions split = " ")) # [1] "hey" "this" "is" "my" "character" "string"