如何从一个 R 向量中提取包含特定子串的字符串?


假设我们有一个包含多个字符串元素的向量,并且我们想要找出哪个字符串元素包含一个特定的子串。这可以通过 grep 函数来实现。例如,如果我们有一个名为 x 的向量,它包含五个不同长度的字符串元素,然后通过使用命令 grep("programming",x,fixed=TRUE) 找出哪个元素具有子串编程元素。

示例

 在线示例

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

输出

[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"

Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.

示例

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

输出

[1] 1 3

示例

 在线示例

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

输出

[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"

示例

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

输出

[1] 1 2 4 7 8

示例

 在线示例

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

输出

[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"

示例

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

输出

[1] 1 2 3

示例

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

输出

[1] 2

更新于: 10-2-2021

1K+ 浏览量

开启您的 职业

完成课程后获得认证

开始
广告