我可以使用 OR 语句来指示 stringr 的 str_extract_all 函数中的模式吗? [英] Can I use an OR statement to indicate the pattern in stringr's str_extract_all function?
本文介绍了我可以使用 OR 语句来指示 stringr 的 str_extract_all 函数中的模式吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在查看数据框中的多个单元格,并试图提取多个字符序列中的任何一个;每个单元格只有这些序列之一.
I'm looking at a number of cells in a data frame and am trying to extract any one of several sequences of characters; there's only one of these sequences per per cell.
我的意思是:
dF$newColumn = str_extract_all(string = "dF$column1", pattern ="sequence_1|sequence_2")
我在这里搞砸了语法吗?我可以用stringr拉这种东西吗?请纠正我的无知!
Am I screwing the syntax up here? Can I pull this sort of thing with stringr? Please rectify my ignorance!
推荐答案
是的,您可以使用 |
因为它表示正则表达式中的逻辑 或.举个例子:
Yes, you can use |
since it denotes logical or in regex. Here's an example:
vec <- c("abc text", "text abc", "def text", "text def text")
library(stringr)
str_extract_all(string = vec, pattern = "abc|def")
结果:
[[1]]
[1] "abc"
[[2]]
[1] "abc"
[[3]]
[1] "def"
[[4]]
[1] "def"
但是,在您的命令中,您应该将 "dF$column1"
替换为 dF$column1
(不带引号).
However, in your command, you should replace "dF$column1"
with dF$column1
(without quotes).
这篇关于我可以使用 OR 语句来指示 stringr 的 str_extract_all 函数中的模式吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文