[R] Fast multiple match function

Enrico Schumann es at enricoschumann.net
Tue Apr 7 21:31:49 CEST 2015

On Mon, 06 Apr 2015, Keshav Dhandhania <kshav.91 at gmail.com> writes:

> Hi,
> I know that one can find all occurrences of x in a vector v by doing
>> which(x == v).
> However, if I need to do this again and again, where v is remaining the
> same, then this is quite inefficient. In my particular case, I need to do
> this millions of times, and length(v) = 100 million.
> Does anyone have suggestion on how to go about it?
> I know of a package called fmatch that does the above for the match
> function. But they don't handle multiple matches.

Perhaps 'match(x, v)' is what you want? In which 'x' may be a vector of
length > 1.

In any case, have you actually tried package 'fastmatch'? The function
'fmatch', which that package provides, is very fast for repeated
lookups in a table 'v'.

Enrico Schumann
Lucerne, Switzerland

More information about the R-help mailing list