- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to extract words from a string vector in R?
To extract words from a string vector, we can use word function of stringr package. For example, if we have a vector called x that contains 100 words then first 20 words can be extracted by using the command word(x,start=1,end=20,sep=fixed(" ")). If we want to start at any other word then starting value will be changed accordingly.
Example
x<-c("R is a programming language and software environment for statistical analysis, graphics representation and reporting. R was created by Ross Ihaka and Robert Gentleman at the University of Auckland, New Zealand, and is currently developed by the R Development Core Team. R is freely available under the GNU General Public License, and pre-compiled binary versions are provided for various operating systems like Linux, Windows and Mac. This programming language was named R, based on the first letter of first name of the two R authors (Robert Gentleman and Ross Ihaka), and partly a play on the name of the Bell Labs Language S.") x
Output
[1] "R is a programming language and software environment for statistical analysis, graphics representation and reporting. R was created by Ross Ihaka and Robert Gentleman at the University of Auckland, New Zealand, and is currently developed by the R Development Core Team. R is freely available under the GNU General Public License, and pre-compiled binary versions are provided for various operating systems like Linux, Windows and Mac. This programming language was named R, based on the first letter of first name of the two R authors (Robert Gentleman and Ross Ihaka), and partly a play on the name of the Bell Labs Language S."
Example
library(stringr) word(x,start=1,end=5,sep=fixed(" "))
Output
[1] "R is a programming language"
Example
word(x,start=1,end=20,sep=fixed(" "))
Output
[1] "R is a programming language and software environment for statistical analysis, graphics representation and reporting. R was created by Ross"
Example
word(x,start=1,end=10,sep=fixed(" "))
Output
[1] "R is a programming language and software environment for statistical"
Example
word(x,start=1,end=15,sep=fixed(" "))
Output
[1] "R is a programming language and software environment for statistical analysis, graphics representation and reporting."
Example
word(x,start=1,end=50,sep=fixed(" "))
Output
[1] "R is a programming language and software environment for statistical analysis, graphics representation and reporting. R was created by Ross Ihaka and Robert Gentleman at the University of Auckland, New Zealand, and is currently developed by the R Development Core Team. R is freely available under the GNU General Public"
Example
word(x,start=11,end=20,sep=fixed(" "))
Output
[1] "analysis, graphics representation and reporting. R was created by Ross"
Example
word(x,start=51,end=60,sep=fixed(" "))
Output
[1] "License, and pre-compiled binary versions are provided for various operating"
Example
word(x,start=6,end=10,sep=fixed(" "))
Output
[1] "and software environment for statistical"
Example
word(x,start=11,end=60,sep=fixed(" "))
Output
[1] "analysis, graphics representation and reporting. R was created by Ross Ihaka and Robert Gentleman at the University of Auckland, New Zealand, and is currently developed by the R Development Core Team. R is freely available under the GNU General Public License, and pre-compiled binary versions are provided for various operating"
Example
word(x,start=1,end=90,sep=fixed(" "))
Output
[1] "R is a programming language and software environment for statistical analysis, graphics representation and reporting. R was created by Ross Ihaka and Robert Gentleman at the University of Auckland, New Zealand, and is currently developed by the R Development Core Team. R is freely available under the GNU General Public License, and pre-compiled binary versions are provided for various operating systems like Linux, Windows and Mac. This programming language was named R, based on the first letter of first name of the two R authors (Robert Gentleman and Ross Ihaka),"
Example
word(x,start=11,end=90,sep=fixed(" "))
Output
[1] "analysis, graphics representation and reporting. R was created by Ross Ihaka and Robert Gentleman at the University of Auckland, New Zealand, and is currently developed by the R Development Core Team. R is freely available under the GNU General Public License, and pre-compiled binary versions are provided for various operating systems like Linux, Windows and Mac. This programming language was named R, based on the first letter of first name of the two R authors (Robert Gentleman and Ross Ihaka),"
Example
word(x,start=21,end=90,sep=fixed(" "))
Output
[1] "Ihaka and Robert Gentleman at the University of Auckland, New Zealand, and is currently developed by the R Development Core Team. R is freely available under the GNU General Public License, and pre-compiled binary versions are provided for various operating systems like Linux, Windows and Mac. This programming language was named R, based on the first letter of first name of the two R authors (Robert Gentleman and Ross Ihaka),"
Example
word(x,start=51,end=100,sep=fixed(" "))
Output
[1] "License, and pre-compiled binary versions are provided for various operating systems like Linux, Windows and Mac. This programming language was named R, based on the first letter of first name of the two R authors (Robert Gentleman and Ross Ihaka), and partly a play on the name of the Bell"
- Related Articles
- How to extract string before slash from a vector in R?
- How to extract all string values from a vector in R with maximum lengths?
- How to extract the names of vector values from a named vector in R?
- How to extract characters from a string in R?
- How to extract the maximum value from named vector in R?
- Extract string vector elements up to a fixed number of characters in R.
- How to extract first two characters from a string in R?
- How to extract a data.table row as a vector in R?
- How to extract strings based on first character from a vector of strings in R?
- How to extract number from string in R data frame?
- How to remove only last character from a string vector in R?
- How to extract the strings between two words in R?
- How to extract initial, last, or middle characters from a string in R?
- How to put space between words that start with uppercase letter in string vector in R?
- How to convert a string vector into an integer vector in R?

Advertisements