从R中的字符串中提取模式,而无需区分大小写字母 [英] Extract pattern from string in R without distinguishing between upper and lower case letters
本文介绍了从R中的字符串中提取模式,而无需区分大小写字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是一个玩具示例.我想在 a
中搜索并提取在 b
中列出的那些颜色.即使颜色不是以大写字母开头,我也要提取它.但是,输出应该告诉我 a
中颜色的使用方式.
This is a toy example. I want to search within a
and extract those colors that are listed in b
. Even if the color does not start with an upper case letter, I want to extract it. However, the output should tell me how the color was used in a
.
所以我想得到的答案是#"Red" NA"blue
.
So the answer I would like to get is #"Red" NA "blue
.
a <- "She has Red hair and blue eyes"
b <- c("Red", "Yellow", "Blue")
str_extract(a, b)#"Red" NA NA
我使用了'stringr'中的 str_extract
,但很乐意使用其他函数/包(例如 grep
).
I used str_extract
from 'stringr', but would be happy to use another function/package (e.g., grep
).
推荐答案
我们可以在 base R
unlist(sapply(tolower(b), function(x) {
x1 <- regmatches(a, gregexpr(x, tolower(a)))
replace(x1, x1 == "character(0)", NA)}), use.names=FALSE)
# "Red" NA "blue"
或者从@leerssej的答案中得到启发
Or as inspired from @leerssej's answer
library(stringr)
str_extract(a, fixed(b, ignore_case=TRUE))
#[1] "Red" NA "blue"
这篇关于从R中的字符串中提取模式,而无需区分大小写字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文