VOOZH about

URL: https://www.geeksforgeeks.org/r-language/split-character-string-at-whitespace-in-r/

⇱ Split Character String at Whitespace in R - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Split Character String at Whitespace in R

Last Updated : 23 Jul, 2025

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

Method 1: Using strsplit() function

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

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

Output:

[1] "Hello"  "Geeks"  "we"     "are"    "in"     "Java"   "Python" "R"      

[9] "and"    "CPP"

Method 2: Using scan() function

This function is also used to split the string by scanning the elements.

Syntax:

scan(text = input_string, what = "")

Where,

  • the text parameter is used to store input string
  • what is a parameter that can take white space which specifies the string to be split at whitespace

It will also display how many times is scanned (It will return the number of split words).

Example: R program to split a string at white space using scan() function

Output:

Read 10 items

[1] "Hello"  "Geeks"  "we"     "are"    "in"     "Java"   "Python" "R"      

[9] "and"    "CPP"

Example: R program to split a string at white space using scan() function

Output:

Read 6 items

[1] "There"    "are"      "big"      "branches" "in"       "India" 

Comment

Explore