How to use strsplit() function In R Language

strsplit() function is used to split the string based on some condition.

Syntax:

strsplit(input_string, ” +”)

where 

  • input_string is the string
  • ” +” represents to be split at white space

Example: R program to split a given string

R




# consider a string with white spaces
string1="Hello Geeks we are in   Java  Python  R and    CPP"
  
# split the string by using strsplit() function
print(strsplit(string1, " +"))  


Output:

[[1]]

[1] “Hello”  “Geeks”  “we”     “are”    “in”     “Java”   “Python” “R”      

[9] “and”    “CPP” 

It is stored in index 1 in a list so we can modify our code a little by using the index 1 position.

Example: R program to split a given string

R




# consider a string with white spaces
string1="Hello Geeks we are in   Java  Python  R and    CPP"
  
# split the string by using strsplit() function
print(strsplit(string1, " +")[[1]])  


Output:

[1] “Hello”  “Geeks”  “we”     “are”    “in”     “Java”   “Python” “R”      

[9] “and”    “CPP”

Split Character String at Whitespace in R

In this article, we are going to discuss how to split character strings at white space in R programming language.

Similar Reads

Method 1: Using strsplit() function

strsplit() function is used to split the string based on some condition....

Method 2: Using scan() function

...

Contact Us