在R中的另一个字符串中找到一个字符串 [英] Find a string in another string in R
问题描述
我想在R中的另一个字符串中找到一个字符串。这些字符串如下。我希望能够将字符串a与字符串b进行匹配,并且输出应该为 a == b
,它返回TRUE
I want to find a string within another string in R. The strings are as follows. I want to be able to match string a to string b as and the out put should be a == b
which returns TRUE
a <- "6250;7250;6251"
b <- "7250"
a == b #FALSE
推荐答案
如果b等于 725
而不是 7250
,您是否仍希望结果为 TRUE
?
If b were to equal 725
instead of 7250
, would you still want the result to be TRUE
?
如果是这样,那么已经给出的 grepl
答案就可以了(您可以通过设置 fixed = TRUE
,因为没有要匹配的模式。
If so then the grepl
answer already given will work (and you could speed it up a bit by setting fixed=TRUE
since there are no patterns to be matched.
如果您只想 TRUE
当之间完全匹配;
时,您要么需要嵌入 b
转换为正则表达式( sprintf
可能有帮助),或更简单的方法是,使用 strsplit
拆分 a
放入要匹配的部分,然后使用%in%
查看是否 b
与这些值中的任何一个都匹配。
If you only want TRUE
when there is an exact match to something between ;
then you will either need to embed b
into a regular expression (sprintf
may be of help), or simpler, use strsplit
to split a
into just the parts to be matched, then use %in%
to see if b
is a match to any of those values.
这篇关于在R中的另一个字符串中找到一个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!