- 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 strings that contains a particular substring in an R vector?

Suppose we have a vector that contains multiple string elements and we want to find out which string element has a particular substring. This can be done with the help of grep function. For example, if we have a vector called x that contains five string elements each of varying length then finding which of the elements has a substring say programming then it can be done by using the command grep("programming",x,fixed=TRUE)

## Example

x1<-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") x1

## 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" [2] "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" [3] "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

grep("programming",x1,fixed=TRUE)

## Output

[1] 1 3

## Example

x2<-c("A function is a set of statements organized together to perform a specific task. R has a large number of in-built functions and the user can create their own functions","In R, a function is an object so the R interpreter is able to pass control to the function, along with arguments that may be necessary for the function to accomplish the actions","The function in turn performs its task and returns control to the interpreter as well as any result which may be stored in other objects","Function Name − This is the actual name of the function. It is stored in R environment as an object with this name","Arguments − An argument is a placeholder. When a function is invoked, you pass a value to the argument. Arguments are optional; that is, a function may contain no arguments. Also arguments can have default values","Function Body − The function body contains a collection of statements that defines what the function does","Return Value − The return value of a function is the last expression in the function body to be evaluated","R has many in-built functions which can be directly called in the program without defining them first. We can also create and use our own functions referred as user defined functions") x2

## Output

[1] "A function is a set of statements organized together to perform a specific task. R has a large number of in-built functions and the user can create their own functions" [2] "In R, a function is an object so the R interpreter is able to pass control to the function, along with arguments that may be necessary for the function to accomplish the actions" [3] "The function in turn performs its task and returns control to the interpreter as well as any result which may be stored in other objects" [4] "Function Name − This is the actual name of the function. It is stored in R environment as an object with this name" [5] "Arguments − An argument is a placeholder. When a function is invoked, you pass a value to the argument. Arguments are optional; that is, a function may contain no arguments. Also arguments can have default values" [6] "Function Body − The function body contains a collection of statements that defines what the function does" [7] "Return Value − The return value of a function is the last expression in the function body to be evaluated" [8] "R has many in-built functions which can be directly called in the program without defining them first. We can also create and use our own functions referred as user defined functions"

## Example

grep("R",x2,fixed=TRUE)

## Output

[1] 1 2 4 7 8

## Example

x3<-c("Matrices are the R objects in which the elements are arranged in a two-dimensional rectangular layout. They contain elements of the same atomic types. Though we can create a matrix containing only characters or only logical values, they are not of much use. We use matrices containing numeric elements to be used in mathematical calculations","A Matrix is created using the matrix() function","data is the input vector which becomes the data elements of the matrix","nrow is the number of rows to be created","ncol is the number of columns to be created","byrow is a logical clue. If TRUE then the input vector elements are arranged by row","dimname is the names assigned to the rows and columns") x3

## Output

[1] "Matrices are the R objects in which the elements are arranged in a two-dimensional rectangular layout. They contain elements of the same atomic types. Though we can create a matrix containing only characters or only logical values, they are not of much use. We use matrices containing numeric elements to be used in mathematical calculations" [2] "A Matrix is created using the matrix() function" [3] "data is the input vector which becomes the data elements of the matrix" [4] "nrow is the number of rows to be created" [5] "ncol is the number of columns to be created" [6] "byrow is a logical clue. If TRUE then the input vector elements are arranged by row" [7] "dimname is the names assigned to the rows and columns"

## Example

grep("matrix",x3,fixed=TRUE)

## Output

[1] 1 2 3

## Example

grep("Matrix",x3,fixed=TRUE)

## Output

[1] 2

- Related Articles
- How to extract strings based on first character from a vector of strings in R?
- How to find the position of minimum value in a vector that contains integers as strings in R?
- How to extract a string that lies between two strings in R?
- How to extract substring from a sting starting at a particular position in MySQL?
- How to extract words from a string vector in R?
- How to remove the first replicate in a vector using another vector that contains similar elements in R?
- How to extract a data.table row as a vector in R?
- How to extract columns based on particular column values of an R data frame that match a pattern?
- How to create a frequency table of a vector that contains repeated values in R?
- How to remove a particular value from a vector in R?
- How to sort a vector in increasing order that contains numbers and characters in R?
- How to extract the names of vector values from a named vector in R?
- How to extract vector using different index for columns in an R matrix?
- How to extract string before slash from a vector in R?
- How to check whether a vector contains an NA value or not in R?

Advertisements