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

 Live Demo

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

 Live Demo

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

 Live Demo

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

Updated on: 10-Feb-2021

819 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements