[R] Re gular Expression help

Wacek Kusnierczyk Waclaw.Marcin.Kusnierczyk at idi.ntnu.no
Sat Nov 8 23:02:31 CET 2008


Gabor Grothendieck wrote:
> For the problem at hand I think I would use your solution
> which is both easily understood and fastest.  On the
> other hand the tapply based solutions are coordinate
> free (i.e. no explicit mucking with indices) and readily
> generalize to more than 2 groups -- just replace [^pq] with
> [^pqr], say.
>
>   

for sure, mine was optimized towards the case, not towards generalizability.
the gsubfn one is a loser, though.

but the first one *is* easily generalizable, e.g.,

letters = "pqrs"
sapply(sprintf("^[^%s]*%s", letters, unlist(strsplit(letters,
split=""))), grep, x=x, value=TRUE)

while an order of magnitude faster than the tapply ones.

vQ



More information about the R-help mailing list