[R] Retrieve regular expression groups

Gabor Grothendieck ggrothendieck at gmail.com
Thu May 6 06:25:25 CEST 2010


Yes, you have to wrap it in a function but note that gsubfn does have
facilities to make wrapping something in a function easier.  If a
function call is preceded by fn$ then a function in the arguments can
be specified using a formula notation.

For example, first we define NAify which forms a list of its arguments
and then replaces NULLs with NAs and then reshapes into a matrix.
Instead of writing function(el) if (is.null(el)) NA else el we wrote
the indicated formula whose right hand side is the function body and
whose arguments are the free variables in the right hand side, in this
case just el.  Later we use formula notation
to specify function(pat) strapply(tmp, pat, c, simplify = NAify) more compactly.

> # replace NULLs with NAs and form into matrix
> NAify <- function(...) {
+ do.call(rbind, fn$lapply(list(...), ~ if (is.null(el)) NA else el))
+ }
>
> do.call(cbind, fn$lapply(pats, pat ~ strapply(tmp, pat, c, simplify = NAify)))
     [,1] [,2]     [,3] [,4]
[1,] "a"  "test"   "a"  "test"
[2,] "a"  "test"   "a"  "test"
[3,] "a"  "test"   NA   NA
[4,] NA   NA       NA   NA
[5,] "a"  "foolio" "a"  "foolio"
[6,] "a"  "foolio" "a"  "foolio"
[7,] NA   NA       "a"  "test"
[8,] NA   NA       "a"  "test"


On Wed, May 5, 2010 at 11:03 PM, OKB (not okblacke)
<brenbarn at brenbarn.net> wrote:
> Gabor Grothendieck wrote:
>
>> Note that X and FUN are also arguments to sapply
>>
>>> args(sapply)
>> function (X, FUN, ..., simplify = TRUE, USE.NAMES = TRUE)
>> NULL
>>
>> 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.
>
>        Ah, I see.  I take it this means it is not possible to use sapply
> with strapply directly.  (I'd have to write a function that wraps
> strapply with different argument names, and then sapply that.)  Anyway,
> I managed to do what I wanted by collapsing my multiple regexes into
> one, so it's good for now.  Thanks for the help.
>
> --
> --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