[R] Gregexpr - extract results with lapply

Gabor Grothendieck ggrothendieck at gmail.com
Tue Nov 7 15:19:29 CET 2006


Try this:

library(gsubfn)
s <-c("ABC", "this WOUld be gOOD")
strapply(s, "[A-Z]{3}", perl = TRUE)

See:
http://code.google.com/p/gsubfn/

On 11/7/06, Lapointe, Pierre <Pierre.Lapointe at nbf.ca> wrote:
> Gregexpr - extract results with lapply
>
> Hello,
>
> I need to extract sequences of three upper case letters in a string. In
> other words, in this string:
>
>                str <-c("ABC", "this WOUld be gOOD")
>
> The result I'm looking for is ABC WOU OOD.
>
> With gregexpr, I can get the position and length of the sequences
>
>                gregexpr('[A-Z]{3}',str,perl=TRUE)
>
>                        [[1]]
>                        [1] 1
>                        attr(,"match.length")
>                        [1] 3
>
>                        [[2]]
>                        [1]  6 16
>                        attr(,"match.length")
>                        [1] 3 3
>
> and with substr, I can extract  the data.  (I use 1 and 3 as start and stop
> in this example, since I haven't figured out how to use the gregexpr
> results.
>
>                substr(str,1,3)
>
> My problem is linking it all together.  I know the answer is probably with
> lapply
>
>                lapply(str,substr,1,3)
>
> but I don't know how to specify the right start and stop positions from the
> gregexpr results.
>
>
> **************************************************
> AVIS DE NON-RESPONSABILITE: Ce document transmis par courrie...{{dropped}}
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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