As a programmer, you may need to work on tons of strings. You will perform concatenation and splitting of them very often. There comes the strsplit() function in R. In a previous article, we have discussed the paste() function to concatenate the strings. Now, let’s see how we can split a string vector using the strsplit().

strsplit() is an exceptional R function, which splits the input string vector into sub-strings. Let’s see how this function works and what are all the ways to perform splitting of the strings in R using the strsplit().


Table of Contents[

hide

]

Strsplit() Function Syntax

Strsplit(): An R Language function which is used to split the strings into substrings with split arguments.

1

strsplit``(x,split,fixed=T)

Where:

  • X = input data file, vector or a stings.
  • Split = Splits the strings into required formats.
  • Fixed = Matches the split or uses the regular expression.

Use strsplit() function in R – Implementation

In this section, let’s see a simple example that shows the use case of the strsplit() function. In this case, the strsplit() function will split the given input into a list of strings or values.

Let’s see how it works.

1

2

df<-(``"R is the statistical analysis language"``)

strsplit``(df, split = " "``)

Output =

"R" "is" "the" "statistical" "analysis" "language"

We have done it! In this way, we can easily split the strings present in the data. One of the best use cases of strsplit() function is in plotting the word clouds. In that, we need tons of word strings to plot the most popular or repeated word. So, in order to get the strings from the data we use this function which returns the list of strings.


1. Using strsplit() function with delimiter

A delimiter in general is a simple symbol, character, or value that separates the words or text in the data. In this section, we will be looking into the use of various symbols as delimiters.

1

2

df<-``"get%better%every%day"

strsplit``(df,split = '%'``)

Output =

"get" "better" "every"  "day"  

In this case, the input text has the % as a delimiter. Now, our concern is to remove the delimiter and get the text as a list of strings. The strsplit() function has done the same here. It removed the delimiter and returned the strings as a list.

#r programming #function

How to use strsplit() function in R? - JournalDev - R Programming
2.05 GEEK