Quick grep with vector pattern or match to return a list of all matches

I think this is trivial, I apologize, I could not find how to do this.

I try to refrain from the cycle, so I try to vectorize the process: I need to do something like grep, but where patternis the vector. Another embodiment is matchwhere value- is not only the first location.

For example, data (which is not real data, otherwise I would use its 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 matchis that it match(x=COUNTRIES_Origin,table=COUNTRIES_Target)returns a vector with the same length as COUNTRIES_Origin, and the value is the first match, while I need them all.

grep , grep(pattern=COUNTRIES_Origin,x=COUNTRIES_Target) : Warning message: In grep(pattern = COUNTRIES_Origin, x = COUNTRIES_Target) : argument 'pattern' has length > 1 and only the first element will be used

?

0
2

, lapply , loop.

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

, grep , .

0

MxN , , , MN.

O (1).

hash . R? , ?

0

All Articles