Open In App

strsplit() Function in R

Improve
Improve
Like Article
Like
Save
Share
Report

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.

Example 1: Using strsplit() function

Here we are going to apply this function in string.

R




# R program to illustrate
# strsplit() function
  
# Initializing a string
String <- ("GFG is a CS Portal.")
  
# Calling the strsplit() function over
# the above string to split the given input
# into a list of strings or values
strsplit(String, " ")


Output :

[[1]]
[1] "GFG"     "is"      "a"       "CS"      "Portal."

Example 2: Using delimiter along with strsplit() function

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 string
String <- ("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 values
strsplit(String, "//")


Output:

[[1]]
[1] "GFG"     "is"      "a"       "CS"      "Portal."

Example 3: Using regular expression along with strsplit() function

 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 string
String <- ("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 values
strsplit(String, "[0-9]+")


Output:

[[1]]
[1] "GeeksforGeeks" "is"            "a"             "Computer"    
[5] "Science"       "Portal."    

Example 4: Split each character of the string

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 string
String <- ("GFG is a CS Portal.")
  
# Calling the strsplit() function over
# the above string to split the given
# input into each characters as string
strsplit(String, "")


Output:

[[1]]

[1] “G” “F” “G” ” ” “i” “s” ” ” “a” ” ” “C” “S” ” ” “P” “o” “r” “t” “a” “l” “.”

Example 5: Split date using this function

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 values
Date_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 column
matrix(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"


Last Updated : 31 Aug, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads