Table of Contents
The str_split function in R is used to split a string into smaller substrings based on a specified delimiter. This function is useful for manipulating and extracting data from strings in a more efficient manner. To use this function, the string and the delimiter must be specified as parameters. The output is a list of substrings. For example, if we have a string Hello, world!” and use the comma as the delimiter, the output will be a list with two elements Hello” and “world!”. This function can be particularly useful in data cleaning and preparation tasks.
Use str_split in R (With Examples)
The str_split() function from the package in R can be used to split a string into multiple pieces. This function uses the following syntax:
str_split(string, pattern)
where:
- string: Character vector
- pattern: Pattern to split on
Similarly, the str_split_fixed() function from the stringr package can be used to split a string into a fixed number of pieces. This function uses the following syntax:
str_split_fixed(string, pattern, n)
where:
- string: Character vector
- pattern: Pattern to split on
- n: Number of pieces to return
This tutorial provides examples of how to use each of these functions on the following data frame:
#create data frame
df <- data.frame(team=c('andy & bob', 'carl & doug', 'eric & frank'),
points=c(14, 17, 19))
#view data frame
df
team points
1 andy & bob 14
2 carl & doug 17
3 eric & frank 19
Example 1: Split String Using str_split()
The following code shows how to split the string in the “team” column using the str_split() function:
library(stringr)#split the string in the team column on " & " str_split(df$team, " & ") [[1]] [1] "andy" "bob" [[2]] [1] "carl" "doug" [[3]] [1] "eric" "frank"
The result is a list of three elements that show the individual player names on each team.
Example 2: Split String Using str_split_fixed()
The following code shows how to split the string in the “team” column into two fixed pieces using the str_split_fixed() function:
library(stringr)
#split the string in the team column on " & "
str_split_fixed(df$team, " & ", 2)
[,1] [,2]
[1,] "andy" "bob"
[2,] "carl" "doug"
[3,] "eric" "frank"Once useful application of the str_split_fixed() function is to append the resulting matrix to the end of the data frame. For example:
library(stringr)
#split the string in the team column and append resulting matrix to data frame
df[ , 3:4] <- str_split_fixed(df$team, " & ", 2)
#view data frame
df
team points V3 V4
1 andy & bob 14 andy bob
2 carl & doug 17 carl doug
3 eric & frank 19 eric frankThe column titled ‘V3’ shows the name of the first player on the team and the column titled ‘V4’ shows the name of the second player on the team.
Cite this article
stats writer (2024). How can we use the str_split function in R?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-we-use-the-str_split-function-in-r-provide-some-examples/
stats writer. "How can we use the str_split function in R?." PSYCHOLOGICAL SCALES, 1 May. 2024, https://scales.arabpsychology.com/stats/how-can-we-use-the-str_split-function-in-r-provide-some-examples/.
stats writer. "How can we use the str_split function in R?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-we-use-the-str_split-function-in-r-provide-some-examples/.
stats writer (2024) 'How can we use the str_split function in R?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-we-use-the-str_split-function-in-r-provide-some-examples/.
[1] stats writer, "How can we use the str_split function in R?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.
stats writer. How can we use the str_split function in R?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
