How can I use the strsplit() function in R to split elements of a string?

How can I use the strsplit() function in R to split elements of a string?

The strsplit() function in R is a useful tool for splitting elements of a string into separate parts. This function takes in a string as input and allows the user to specify a delimiter or separator to split the string into multiple components. By utilizing this function, the user can easily break down a complex string into smaller, more manageable parts. This can be especially helpful for manipulating data or text in a specific format. Overall, the strsplit() function is an efficient way to extract and manipulate individual elements of a string in R.

Use strsplit() Function in R to Split Elements of String


The strsplit() function in R can be used to split a string into multiple pieces. This function uses the following syntax:

strsplit(string, pattern)

where:

  • string: Character vector
  • pattern: Pattern to split on

The following examples show how to use this function in practice.

Example 1: Split String Based on Spaces

The following code shows how to use the strsplit() function to split a string based on spaces:

#split string based on spaces
split_up <- strsplit("Hey there people", split=" ")

#view results
split_up

[[1]]
[1] "Hey"    "there"  "people"

#view class of split_up
class(split_up)

[1] "list"

The result is a list of three elements that are split based on the spaces in the original string.

We can use the unlist() function if we would instead like to produce a vector as the result:

#split string based on spaces
split_up <- unlist(strsplit("Hey there people", split=" "))

#view results
split_up

[1] "Hey"    "there"  "people"

#view class of split_up
class(split_up)

[1] "character"

We can see that the result is a character vector.

Example 2: Split String Based on Custom Delimiter

We can also use the strplit() function to split a string based on a custom delimiter, such as a dash:

#split string based on dashes
strsplit("Hey-there-people", split="-")

[[1]]
[1] "Hey"    "there"  "people"

The result is a list of three elements that are split based on the dashes in the original string.

Example 3: Split String Based on Several Delimiters

#split string based on several delimiters
strsplit("Hey&there-you/people", split="[&-/]")

[[1]]
[1] "Hey"    "there"  "you"    "people"

The result is a list of elements that were split whenever any of the following delimiters were present in the original string:

  • Ampersand (&)
  • Dash ()
  • Slash (/)

Additional Resources

The following tutorials explain how to perform other common operations with strings in R:

How to Perform Partial String Matching in R

Cite this article

stats writer (2024). How can I use the strsplit() function in R to split elements of a string?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-use-the-strsplit-function-in-r-to-split-elements-of-a-string/

stats writer. "How can I use the strsplit() function in R to split elements of a string?." PSYCHOLOGICAL SCALES, 29 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-use-the-strsplit-function-in-r-to-split-elements-of-a-string/.

stats writer. "How can I use the strsplit() function in R to split elements of a string?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-use-the-strsplit-function-in-r-to-split-elements-of-a-string/.

stats writer (2024) 'How can I use the strsplit() function in R to split elements of a string?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-use-the-strsplit-function-in-r-to-split-elements-of-a-string/.

[1] stats writer, "How can I use the strsplit() function in R to split elements of a string?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I use the strsplit() function in R to split elements of a string?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top