用矢量模式或匹配快速grep,返回所有匹配的列表 [英] Fast grep with a vectored pattern or match, to return list of all matches

查看:131
本文介绍了用矢量模式或匹配快速grep,返回所有匹配的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想这是微不足道的,我很抱歉,我找不到该怎么做。



我试图戒掉一个循环,所以我尝试向量化过程:
我需要做一些类似于 grep 的事情,但是模式是一个向量。另一个选项是匹配,其中不仅是第一个位置。



例如数据(这不是真实的数据是怎么样的,另外我会利用它的结构):

  COUNTRIES = c(奥地利,比利时,丹麦,法国,德国,
爱尔兰,意大利,卢森堡,荷兰,
葡萄牙,瑞典,西班牙,芬兰,英国)

COUNTRIES_Target = rep(COUNTRIES,times = 4066)
COUNTRIES_Origin = rep(COUNTRIES,each = 4066)

现在,我得到了一个循环:

  var_pointer = list()
for(i in 1:length(COUNTRIES_Origin))
{
var_pointer [[i]] = which(COUNTRIES_Origin [i] == COUNTRIES_Target)
}

c $ c> match 是 match(x = COUNTRIES_Origin,table = COUNTRIES_Target)返回一个长度与 COUNTRIES_Origin ,值是第一个匹配,而我需要他们所有人。



grep 的问题在于 grep(pattern = COUNTRIES_Origin,x = COUNTRIES_Target)是给定的警告:
警告消息:
在grep中(pattern = COUNTRIES_Origin,x = COUNTRIES_Target):
参数'pattern'的长度> 1,只有第一个元素会被使用



有什么建议?

解决方案

看起来好像你可以在列表中而不是 loop 。< (COUNTRIES_Origin,function(x)which(COUNTRIES_Target == x))
$ / $>

  

在这里我使用哪个因为 grep 似乎对部分匹配更好,而您正在寻找完全匹配。


I guess this is trivial, I apologize, I couldn't find how to do it.

I am trying to abstain from a loop, so I am trying to vectorize the process: I need to do something like grep, but where the pattern is a vector. Another option is a match, where the value is not only the first location.

For example data (which is not how the real data is, otherswise I would exploit it structure):

COUNTRIES=c("Austria","Belgium","Denmark","France","Germany",
"Ireland","Italy","Luxembourg","Netherlands",
"Portugal","Sweden","Spain","Finland","United Kingdom")

COUNTRIES_Target=rep(COUNTRIES,times=4066)
COUNTRIES_Origin=rep(COUNTRIES,each=4066)

Now, currently I got a loop that:

var_pointer=list()
for (i in 1:length(COUNTRIES_Origin))
{     
var_pointer[[i]]=which(COUNTRIES_Origin[i]==COUNTRIES_Target)
 }

The problem with match is that match(x=COUNTRIES_Origin,table=COUNTRIES_Target) returns a vector of the same length as COUNTRIES_Origin and the value is the first match, while I need all of them.

The issue with grep is that grep(pattern=COUNTRIES_Origin,x=COUNTRIES_Target) is the given warning: Warning message: In grep(pattern = COUNTRIES_Origin, x = COUNTRIES_Target) : argument 'pattern' has length > 1 and only the first element will be used

Any suggestions?

解决方案

It seems like you can just lapply over the list rather than loop.

lapply(COUNTRIES_Origin, function(x) which(COUNTRIES_Target==x))

Here I use which because grep seems to be better for partial matches and you're looking for exact matches.

这篇关于用矢量模式或匹配快速grep,返回所有匹配的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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