Written and maintained by CASRAI Editorial Board
Published · Last updated
On this page
strsplit() is the base R function that cuts each element of a character vector into pieces wherever a separator pattern matches, and it always returns a list with one element per input string. Call it as strsplit(x, split), for example strsplit(“P01_A_2024”, “_”), which gives the three pieces “P01”, “A” and “2024”. Because the split argument is a regular expression by default, a few separators behave differently from what many people expect, and those cases cause most of the confusion around the function.
What strsplit() is used for in research
Research data arrive with several facts packed into one text field: a sample ID such as participant, group and year joined by underscores, a “last name, first name” column, a semicolon-separated list of keywords in an exported bibliographic record, or a file name that encodes a site and a date. strsplit() unpacks these fields so each fact can sit in its own variable. It belongs to the early, unglamorous stage of an analysis that the data cleaning steps guide describes, and it is what you reach for before reshaping or modelling.
The function is part of base R, so no package is needed. Users who prefer the tidyverse have close equivalents in the stringr package, covered under free alternatives below; the stringr cheat sheet lists the wider family of string helpers.
How to install or access it
strsplit() ships with R itself, in the base package, and is available in every R session without library(). R is distributed from the Comprehensive R Archive Network at https://cran.r-project.org/. To read the built-in help, run ?strsplit in the console.
The stringr alternatives need one installation from CRAN, run once, then loaded in each session:
install.packages("stringr")
library(stringr)
Step-by-step: split strings and keep the piece you need
The signature in the R reference manual is:
strsplit(x, split, fixed = FALSE, perl = FALSE, useBytes = FALSE)
- x: a character vector; each element is split. Other inputs, including a factor, give an error.
- split: a character vector of regular expressions (exact strings when fixed = TRUE). If it has more than one element it is recycled along x.
- fixed: if TRUE, match split exactly instead of as a regular expression. It has priority over perl.
- perl: if TRUE, use Perl-compatible regular expressions.
- useBytes: if TRUE, match byte by byte instead of character by character.
Step 1. Split one string
- Create a character vector:
id = "P01_A_2024". - Split it on the underscore:
strsplit(id, "_"). - The result is a list holding one character vector:
"P01" "A" "2024". To get the plain vector, take the first list element:strsplit(id, "_")[[1]], or wrap the call inunlist().
Step 2. Split a whole column of IDs
- Start with a vector:
ids = c("P01_A_2024", "P02_B_2025", "P03_A_2025"). - Split every element at once:
parts = strsplit(ids, "_"). The listpartshas three elements, one per ID. - Pull out the same position from every element with the subsetting function:
sapply(parts, `[`, 1)returns"P01" "P02" "P03", andsapply(parts, `[`, 3)returns"2024" "2025" "2025".
Step 3. Turn the list into data frame columns
When every string splits into the same number of pieces, stack the list into a matrix and convert it:
df = as.data.frame(do.call(rbind, parts))
names(df) = c("participant", "group", "year")
The result has three rows and three character columns. If you plan to read the year as a number, convert it afterwards with df$year = as.integer(df$year), because strsplit() only ever produces text. When the strings do not all split into the same number of pieces, rbind() recycles shorter vectors and gives a warning rather than an error, so check lengths first with table(lengths(parts)). For data that arrive in a CSV file, see the guide to reading CSV files into R, and for column-wise reshaping afterwards the tidyr package guide.
Splitting on a pattern, a literal or single characters
- A regular expression:
strsplit("a1b22c", "[0-9]+")splits at each run of digits and returns"a" "b" "c". - A literal string: add fixed = TRUE, as in
strsplit("a.b.c", ".", fixed = TRUE), which returns"a" "b" "c". - Single characters:
strsplit("abc", "")returns"a" "b" "c". Per the manual, split = “” and split = character(0) are equivalent, and what counts as a “character” depends on the locale. - Perl-style patterns: add perl = TRUE when you need lookaheads or other Perl-compatible syntax.
Output and how to read it
strsplit() returns a list of the same length as x, and the i-th element holds the pieces of x[i]. A list is used because different strings can break into different numbers of pieces. This is why strsplit("a b c", " ") prints as [[1]] followed by the vector, and why the result of a multi-string call must be unpacked with sapply(), unlist() or do.call(rbind, ...) before it can be a column.
The manual describes the algorithm in a short loop: if the string is empty, stop; if there is a match, add the text to the left of the match to the output and discard the match and everything to its left; if there is no match, add the rest of the string and stop. Three consequences follow, and the data table below records each with its documented result:
- A separator at the start of a non-empty string produces an empty first element,
"". - A separator at the end of a string produces nothing extra: the result is the same as if the final separator were removed. A trailing delimiter therefore does not create a trailing empty value, which matters when you count fields.
- An empty input string returns
character(0), not"". A one-space string split on a space returns a single empty string.
Two adjacent separators in the middle of a string give an empty piece between them, for example strsplit("a,,b", ",") returns "a" "" "b". If your data may contain empty pieces you do not want, drop them with parts[[1]][parts[[1]] != ""].
Troubleshooting
Splitting on “.” returns empty strings
The split argument is a regular expression, and in a regular expression “.” matches any single character. The manual’s own example shows that unlist(strsplit("a.b.c", ".")) returns five empty strings. Use "[.]", an escaped dot, or fixed = TRUE; the last two the manual shows return "a" "b" "c". The same applies to other regex metacharacters such as “|”, “(“, “+” and “?”: either escape them, put them in square brackets, or use fixed = TRUE.
Error when the input is a factor
x must be a character vector. A factor gives an error, so convert first: strsplit(as.character(df$id), "_"). This is common when an older script read a CSV with strings turned into factors.
An unexpected empty first element
Leading separators, such as a leading space or a leading comma, produce "" as the first piece. Trim the string first with trimws() or remove empty pieces afterwards.
NA values
A missing value in split leaves the corresponding element of x unsplit. Check your separator vector for NA if some rows come back whole.
Uneven numbers of pieces
If some IDs have two pieces and others three, a simple rbind gives warnings and recycled values. Count pieces with lengths(parts), then fix the source strings or choose a stringr function that pads or caps the pieces, described next.
Free alternatives
All of the functions below are free to use. The stringr functions come from the tidyverse’s stringr package, and the table summarises what its documentation says each one returns.
| Function | Package | Returns | Useful when |
|---|---|---|---|
| strsplit(x, split) | base R | A list of character vectors | You need no extra package |
| str_split(string, pattern, n = Inf, simplify = FALSE) | stringr | A list of character vectors, or a matrix when simplify = TRUE | You want a cap on the number of pieces (n) |
| str_split_1(string, pattern) | stringr | A character vector for one string | You have a single string |
| str_split_fixed(string, pattern, n) | stringr | A character matrix with exactly n columns | You want a fixed number of columns, padded with empty strings |
| str_split_i(string, pattern, i) | stringr | A character vector holding the i-th piece of each string | You need one position; a negative i counts from the right |
Two differences from strsplit() matter in practice. In stringr, pattern is interpreted as a regular expression by default, but you can wrap it in fixed(), coll() or boundary() to change the matching, and an empty pattern splits at character boundaries. When you cap the pieces with n, the last piece keeps the remaining text. str_split_i() replaces the sapply(parts, `[`, i) idiom of Step 2 with one call: str_split_i(ids, "_", 1) returns the participant codes directly. For the rest of the tidyverse workflow, see the dplyr package guide, and for a broader view of no-cost tools see free statistical software.
Frequently asked questions
What does strsplit do in R?
It splits every element of a character vector into substrings at each match of a separator and returns a list of the same length as the input, with one character vector of pieces per input element.
What is the difference between strsplit r and strsplit in R?
None: both searches refer to the same base R function. It is documented in the R reference manual under the base package as strsplit(x, split, fixed = FALSE, perl = FALSE, useBytes = FALSE).
Why does strsplit(“a.b.c”, “.”) return empty strings?
Because split is treated as a regular expression and “.” matches any character. Use “[.]” or fixed = TRUE to split on a literal dot.
How do I get the first piece of every string?
Use sapply(strsplit(x, "_"), `[`, 1) in base R, or str_split_i(x, "_", 1) in stringr.
How do I split a string into single characters?
Use strsplit(x, ""). The manual states that split = “” and split = character(0) are equivalent.
Sources
- R Core Team, R reference manual, strsplit {base}: https://stat.ethz.ch/R-manual/R-devel/library/base/html/strsplit.html (checked 2026-11-15)
- Tidyverse, stringr reference, str_split: https://stringr.tidyverse.org/reference/str_split.html (checked 2026-11-15)
- The Comprehensive R Archive Network: https://cran.r-project.org/ (checked 2026-11-15)
Data: strsplit() edge cases: documented results for common split patterns
Compiled from the R reference manual for strsplit {base}: the documented result of splitting specific example strings, covering regular-expression versus fixed matching, leading and trailing separators, and empty or single-space inputs.
| Call | Input string | Result | Rule that explains it |
|---|---|---|---|
| strsplit(x, ".") | a.b.c | five empty strings ("") | split is a regular expression and "." matches every character |
| strsplit(x, "[.]") | a.b.c | "a" "b" "c" | bracketed dot matches a literal dot |
| strsplit(x, ".", fixed = TRUE) | a.b.c | "a" "b" "c" | fixed = TRUE matches split exactly |
| strsplit(x, "#") | #a# | "" "a" | match at start gives empty first element; match at end adds nothing |
| strsplit(x, " ") | (empty string) | character(0) | an empty input string yields no pieces |
| strsplit(x, " ") | (one space) | "" | the single match leaves an empty piece to its left |
Sources
- R reference manual: strsplit {base} (accessed 2026-11-15)
Compiled by CASRAI from the sources above. Free to reuse under CC BY 4.0 with attribution.







