Splitting a string variable breaks it into sub-strings by the given delimiter. For example, splitting “volley-ball” will result in “volley ball”. This guide will explain how to split strings in R.
Splitting Strings in R using strsplit() method
strsplit() method breaks the string variable by a given delimiter.
Syntax:
strsplit(x, split)
Arguments:
- x = string that you want to split.
- Split = character string or delimiter to use as a split.
Example:
The following examples show how to use strstplit() function to split strings with whitespace, a character, and a string itself.
#creating variables
string1 <- "I Love R Programming"
string2 <- "123_456_789"
string3 <- "Asia and North America and Australia"
#splitting strings with whitespace
strsplit(string1, split = " ")
#splitting strings with a character
strsplit(string2, split = "_")
#splitting strings with a string
strsplit(string3, split = "and")
Output:
![Output](https://codingcampus.net/wp-content/uploads/2022/07/Output-1-600x237-1.png)