Split Character String at Whitespace in R
Last Updated :
23 Sep, 2021
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
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"
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
R
# consider a string with white spaces
string1="Hello Geeks we are in Java Python R and CPP"
# split the string by using scan() function
print(scan(text = string1, what = ""))
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
R
# consider a string with white spaces
string1="There are big branches in India"
# split the string by using scan() function
print(scan(text = string1, what = ""))
Output:
Read 6 items
[1] "There" "are" "big" "branches" "in" "India"
Similar Reads
Remove All White Space from Character String in R In this article, we are going to see how to remove all the white space from character string in R Programming Language. We can do it in these ways: Using gsub() function.Using str_replace_all() function.Method 1: Using gsub() Function gsub() function is used to remove the space by removing the space
2 min read
Remove All Special Characters from String in R In this article, we are going to remove all special characters from strings in R Programming language. For this, we will use the str_replace_all() method to remove non-alphanumeric and punctuations which is available in stringr package. Installation To install this library type the below command in
1 min read
Replace Specific Characters in String in R In this article, we will discuss how to replace specific characters in a string in R Programming Language. Method 1: Using gsub() function We can replace all occurrences of a particular character using gsub() function. Syntax: gsub(character,new_character, string)Â Parameters: string is the input st
3 min read
How to Check if Characters are Present in a String in R. In this article, we will learn how to check the presence of a character, substring or number in a string using R Programming Language. This can be useful for tasks where we want to filter data, pattern matching or data cleaning. We generally use grepl() function, a versatile tool that is used for ch
6 min read
Extract Numbers from Character String Vector in R In this article, we are going to see how to extract Numbers from Character String Vector in R Programming Language. There are different approaches to extract numbers from character string vectors using some in-built functions. It can be done in the following ways:Using the gsub() functionUsing the g
2 min read