本文介绍了使用 grepl 搜索文本中的多个子字符串之一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在 R 中使用 grepl() 来搜索我的文本中是否存在以下任一流派.我现在就是这样做的:
I am using grepl() in R to search if either of the following genres exist in my text. I am doing it like this right now:
grepl("Action", my_text) |
grepl("Adventure", my_text) |
grepl("Animation", my_text) |
grepl("Biography", my_text) |
grepl("Comedy", my_text) |
grepl("Crime", my_text) |
grepl("Documentary", my_text) |
grepl("Drama", my_text) |
grepl("Family", my_text) |
grepl("Fantasy", my_text) |
grepl("Film-Noir", my_text) |
grepl("History", my_text) |
grepl("Horror", my_text) |
grepl("Music", my_text) |
grepl("Musical", my_text) |
grepl("Mystery", my_text) |
grepl("Romance", my_text) |
grepl("Sci-Fi", my_text) |
grepl("Sport", my_text) |
grepl("Thriller", my_text) |
grepl("War", my_text) |
grepl("Western", my_text)
有没有更好的方法来编写这段代码?我可以将所有类型放在一个数组中,然后以某种方式使用 grepl()
吗?
Is there a better way to write this code? Can I put all the genres in an array and then somehow use grepl()
on that?
推荐答案
您可以使用或"|
分隔符将流派粘贴在一起,然后通过 grepl
运行它单个正则表达式.
You could paste the genres together with an "or" |
separator and run that through grepl
as a single regular expression.
x <- c("Action", "Adventure", "Animation", ...)
grepl(paste(x, collapse = "|"), my_text)
这是一个例子.
x <- c("Action", "Adventure", "Animation")
my_text <- c("This one has Animation.", "This has none.", "Here is Adventure.")
grepl(paste(x, collapse = "|"), my_text)
# [1] TRUE FALSE TRUE
这篇关于使用 grepl 搜索文本中的多个子字符串之一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!