0
0
R Programmingprogramming~5 mins

strsplit in R Programming

Choose your learning style9 modes available
Introduction
The strsplit function breaks a string into smaller parts using a separator you choose. This helps you work with pieces of text separately.
You have a sentence and want to get each word separately.
You receive data where values are joined by commas and want to separate them.
You want to split a list of email addresses separated by semicolons.
You need to analyze parts of a URL separated by slashes.
Syntax
R Programming
strsplit(x, split, fixed = FALSE, perl = FALSE, useBytes = FALSE)
x is the string or vector of strings you want to split.
split is the character or pattern where you want to cut the string.
Examples
Splits the string at each comma, giving a list of fruits.
R Programming
strsplit("apple,banana,cherry", ",")
Splits the string at each space to get individual words.
R Programming
strsplit("one two three", " ")
Splits the string at each dash.
R Programming
strsplit("a-b-c-d", "-")
Sample Program
This program splits the string 'red,green,blue' at commas and prints the parts as a list.
R Programming
text <- "red,green,blue"
result <- strsplit(text, ",")
print(result)
OutputSuccess
Important Notes
strsplit returns a list, even if you split one string.
If you split multiple strings at once, you get a list with one element per string.
Use double backslashes \\ if you want to split by special characters like dot (.) or backslash (\\).
Summary
strsplit breaks strings into parts using a separator.
It returns a list of character vectors with the split pieces.
Useful for extracting words or values from combined text.