部分字符串匹配 - R [英] partial string matching - R

查看:310
本文介绍了部分字符串匹配 - R的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在R中编写一个查询来匹配列名中的部分字符串。我正在寻找类似于SQL中的LIKE运算符的东西。例如,如果我知道字符串的开始,中间或结尾部分,我会以格式写入查询:

I need to write a query in R to match partial string in column names. I am looking for something similar to LIKE operator in SQL. For e.g, if I know beginning, middle or end part of the string I would write the query in format:

LIKE 'beginning%middle%' 

在SQL中,它会返回匹配的字符串。在 pmatch grep 中,我只能指定'开始','结束'而不是顺序。有没有类似的功能在R,我正在寻找?

in SQL and it would return matching strings. In pmatch or grep it seems I can only specify 'beginning' , 'end' and not the order. Is there any similar function in R that I am looking for?

例如,我正在寻找向量:

For example, say I am looking in the vector:

y<- c("I am looking for a dog",
      "looking for a new dog", "a dog", "I am just looking")

假设我想写一个查询寻找一个新狗,我知道字符串的开头是looking,字符串的结尾是dog code>。如果我做了 grep(dog,y),它会返回 1,2,3 。有没有什么办法可以在 grep ?中指定开始和结束?

Lets say I want to write a query which picks "looking for a new dog" and I know start of the string is "looking" and end of string is "dog". If I do a grep("dog",y) it will return 1,2,3. Is there any way I can specify beginning and end in grep?

推荐答案

grep 函数支持正则表达式和正则表达式,你几乎可以匹配任何东西

The grep function supports regular expressions and with regular expressions, you can match almost anything

y<- c("I am looking for a dog", "looking for a new dog", "a dog", "I am just looking")
grep("looking.*dog",y, value=T)
# [1] "I am looking for a dog" "looking for a new dog" 

这里这个模式寻找寻找然后也许某事然后 dog 。所以这应该做你想做的。

Here this pattern looks for looking then "maybe something" then dog. So that should do what you want.

这篇关于部分字符串匹配 - R的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆