strsplit() Function in R

31 Aug, 2021
The strsplit() in R programming language function is used to split the elements of the specified character vector into substrings according to the given substring taken as its parameter.
Syntax: strsplit(x, split, fixed = FALSE, perl = FALSE, useBytes = FALSE)
Parameters: This function accepts some parameters which are illustrated below:
x: This is the character vector, data file, or a string whose each element is going to be split.
split: This parameter says to split the specified string X into the required formats.
fixed: This parameter accepts logical values. If its value is TRUE, it matches and splits exactly, otherwise use regular expressions.
perl: This parameter accepts logical values.
useBytes: This parameter accepts logical values. If its value is TRUE, the matching is done byte-by-byte rather than character-by-character, and inputs with marked encodings are not converted.
Return value: This function returns a new split string.
Here we are going to apply this function in string.
R
# R program to illustrate# strsplit() function  # Initializing a stringString <- ("GFG is a CS Portal.")  # Calling the strsplit() function over# the above string to split the given input# into a list of strings or valuesstrsplit(String, " ")
Output :
[[1]]
[1] "GFG"     "is"      "a"       "CS"      "Portal."
The delimiter is nothing but a character or value that is used to separate the words or text in the data. In the below example, strsplit() function is used with delimiter “//” that separates the words separated by the delimiter “//”.
R
# R program to illustrate# strsplit() function  # Initializing a stringString <- ("GFG//is//a//CS//Portal.")  # Calling the strsplit() function over# the above string to split the given# input separated by "//"# into a list of strings or valuesstrsplit(String, "//")
Output:
[[1]]
[1] "GFG"     "is"      "a"       "CS"      "Portal."
 In the below example, the texts are separated with the regular expression “[0-9]+” and strsplit() function is used to split those texts that are separated by that regular expression.
R
# R program to illustrate# strsplit() function  # Initializing a stringString <- ("GeeksforGeeks13is456a246Computer9Science10Portal.")  # Calling the strsplit() function over# the above string to split the given# input separated by the regular expression "[0-9]+"# into a list of strings or valuesstrsplit(String, "[0-9]+")
Output:
[[1]]
[1] "GeeksforGeeks" "is"            "a"             "Computer"    
[5] "Science"       "Portal."    
In the below example, each character of the input string is being split with the help of strsplit() function.
R
# R program to illustrate# strsplit() function  # Initializing a stringString <- ("GFG is a CS Portal.")  # Calling the strsplit() function over# the above string to split the given# input into each characters as stringstrsplit(String, "")
Output:
[[1]]
[1] “G” “F” “G” ” ” “i” “s” ” ” “a” ” ” “C” “S” ” ” “P” “o” “r” “t” “a” “l” “.”
In the below example, strsplit() function is used to split the specified date into each part like date, month, and year.
R
# Initializing some date valuesDate_values <- c("20-08-2021", "12-07-2019",                 "02-05-2020")  # Calling the strsplit() function over the above# specified date values along with the split# format of "-"Result <- strsplit(Date_values, split = "-")  # Getting the split date Result  # Getting the split date in the form of # matrix of 3 columnmatrix(unlist(Result), ncol=3, byrow=T)
Output:
[[1]]
[1] "20"   "08"   "2021"
[[2]]
[1] "12"   "07"   "2019"
[[3]]
[1] "02"   "05"   "2020"

    [,1] [,2] [,3]  
[1,] "20" "08" "2021"
[2,] "12" "07" "2019"
[3,] "02" "05" "2020"
Picked
R String-Functions
R Language
R Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Filter data by multiple conditions in R using Dplyr
How to Replace specific values in column in R DataFrame ?
Change Color of Bars in Barchart using ggplot2 in R
How to Split Column Into Multiple Columns in R DataFrame?
Loops in R (for, while, repeat)
How to Replace specific values in column in R DataFrame ?
How to Split Column Into Multiple Columns in R DataFrame?
How to change Row Names of DataFrame in R ?
How to filter R DataFrame by values in a column?
Remove rows with NA in one column of R DataFrame