[R] Retrieve regular expression groups

OKB (not okblacke) brenbarn at brenbarn.net
Mon May 3 02:20:50 CEST 2010

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

More information about the R-help mailing list