There is strsplit() function in R used to split element in string with specified delimiter.

The following method shows how you can do it with syntax.

Method 1: Use strsplit() Function

strsplit(string, pattern)

The following example shows how to use strsplit() function in R:

Apply strsplit() Function on Character Vector

Let’s see how we can split character vector using strsplit() function based on comma:

# Split string based on spaces
str <- "R, Python, Java, C++, Ruby"

# Split string
split_str <- strsplit(str,",")

# Show output
print(split_str)

# Check class of string
class(split_str)

Output:

[[1]]
[1] "R"      "Python" "Java"   "C++"    "Ruby"  

[1] "list"

Here the above output shows string is splitted into list based comma.