VOOZH about

URL: https://www.geeksforgeeks.org/r-language/strsplit-function-in-r/

⇱ strsplit() Function in R - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

strsplit() Function in R

Last Updated : 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.

Example 1: Using strsplit() function

Here we are going to apply this function in 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 "//".

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.

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.

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.

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"
Comment
Article Tags:

Explore