[R] Retrieve regular expression groups

Gabor Grothendieck ggrothendieck at gmail.com
Mon May 3 03:28:51 CEST 2010

There are quite a few examples in

(1) ?strapply,
(2) on the home page and
(3) in the vignette
(4) on r-help back posts

if you having problems with understanding the textual description.

Note that X and FUN are also arguments to sapply

> args(sapply)
function (X, FUN, ..., simplify = TRUE, USE.NAMES = TRUE)

so the sapply construct in your post has the effect of applying c to
tmp, pats and strapply so the output you observe is correct.  The
sapply  command never even calls strapply.

On Sun, May 2, 2010 at 8:20 PM, OKB (not okblacke)
<brenbarn at brenbarn.net> wrote:
> Gabor Grothendieck wrote:
>> The strapply function in gsubfn does that.  See
>> http://gsubfn.googlecode.com
>        Ah, thanks.  The documentation for that function is pretty
> difficult to grasp, but I think I figured it out. . . almost.  However,
> for some reason I can't seem to make strapply work inside an sapply (to
> do multiple regex searches over the same data).  For instance, take a
> look at this toy setup.
>> pats
> [1] "([^ ]+) .i. ([^ ]+)" "([^ ]+) ..g ([^ ]+)"
>> tmp
> [1] "this is a big test"   "this is a pig test"   "this is a lim test"
> "this is a non test"   "this is a big foolio"
> [6] "this is a wig foolio" "this is a fog test"   "this is a bog test"
>        With these data, strapply(tmp, pats[1], c) works as expected, as
> does strapply(tmp, pats[2], c).  However, this doesn't work:
> sapply(pats, strapply, X=tmp, FUN=c)
> Instead it returns a strange table, some of whose elements contain the
> code of strapply itself.  Also, the above code gives different results
> depending on whether I specify "X=tmp" or simply "tmp" as the third
> argument.  Shouldn't these be the same, since X is the first argument of
> strapply?  Any idea what's going on here?
> Thanks again,
> --
> --OKB (not okblacke)
> Brendan Barnwell
> "Do not follow where the path may lead.  Go, instead, where there is
> no path, and leave a trail."
>        --author unknown
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list